Skip to content
Advertisement

Parse XML tag into array not depend on XML structure [closed]

Which is the quickest way in PHP to parse “category_fullname” texsts into array not depend on XML structure ?

...
<CATEGORY_ID>720</CATEGORY_ID>
<CATEGORY_NAME>Autorádiá</CATEGORY_NAME>
<CATEGORY_FULLNAME>Heureka.sk | Auto-moto | Autorádiá</CATEGORY_FULLNAME>
</CATEGORY>
<CATEGORY>
<CATEGORY_ID>881</CATEGORY_ID>
<CATEGORY_NAME>Alkohol testery</CATEGORY_NAME>
<CATEGORY_FULLNAME>Heureka.sk | Auto-moto | Alkohol testery</CATEGORY_FULLNAME>
</CATEGORY>
<CATEGORY>
<CATEGORY_ID>972</CATEGORY_ID>
<CATEGORY_NAME>Pneumatiky</CATEGORY_NAME>
<CATEGORY_FULLNAME>Heureka.sk | Auto-moto | Pneumatiky</CATEGORY_FULLNAME>
</CATEGORY>
<CATEGORY>
<CATEGORY_ID>1270</CATEGORY_ID>
<CATEGORY_NAME>Strešné boxy</CATEGORY_NAME>
<CATEGORY_FULLNAME>Heureka.sk | Auto-moto | Strešné boxy</CATEGORY_FULLNAME>
</CATEGORY>
<CATEGORY>
<CATEGORY_ID>1271</CATEGORY_ID>
<CATEGORY_NAME>Strešné nosiče</CATEGORY_NAME>
<CATEGORY>
<CATEGORY_ID>1268</CATEGORY_ID>
<CATEGORY_NAME>Nosiče lyží</CATEGORY_NAME>
<CATEGORY_FULLNAME>Heureka.sk | Auto-moto | Strešné nosiče | Nosiče lyží</CATEGORY_FULLNAME>
</CATEGORY>
<CATEGORY>
<CATEGORY_ID>1269</CATEGORY_ID>
<CATEGORY_NAME>Nosiče bicyklov</CATEGORY_NAME>
<CATEGORY_FULLNAME>Heureka.sk | Auto-moto | Strešné nosiče | Nosiče bicyklov</CATEGORY_FULLNAME>
</CATEGORY>
<CATEGORY>
<CATEGORY_ID>1312</CATEGORY_ID>
<CATEGORY_NAME>Nosiče vodné športy</CATEGORY_NAME>
<CATEGORY_FULLNAME>Heureka.sk | Auto-moto | Strešné nosiče | Nosiče vodné športy</CATEGORY_FULLNAME> ...

Array should looks like :

Heureka.sk | Auto-moto | Pneumatiky

Heureka.sk | Auto-moto | Strešné boxy

Heureka.sk | Auto-moto | Strešné nosiče | Nosiče lyží

Advertisement

Answer

The quickest way is to use simpleXML, array_map and xpath.

$xml = simplexml_load_file("test.xml")

$arrayCatNames = array_map('strval', $xml->xpath('//CATEGORY_FULLNAME'));

$xml->xpath returns an array of simple XML elements. ‘//CATEGORY_FULLNAME’ searches for the tag anywhere in the XML. With array_map the function strval is applied to all elements to get the values.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement