I’m having some difficulties getting PHP to read my XML document. I’m trying to echo the content from each node based on whatever catid I’ve selected.
XML: text.xml
<root>
<category catid='1'>
<text id='TXT1'><![CDATA[ Lorem Ipsum ]]></text>
<text id='TXT2'><![CDATA[ Lorem Ipsum ]]></text>
<text id='TXT3'><![CDATA[ Lorem Ipsum ]]></text>
</category>
<category catid='2'>
<text id='TXT1'><![CDATA[ Lorem Ipsum ]]></text>
<text id='TXT2'><![CDATA[ Lorem Ipsum ]]></text>
<text id='TXT3'><![CDATA[ Lorem Ipsum ]]></text>
</category>
</root>
PHP:
<?php
$xml = simplexml_load_file('/path/to/text.xml');
$category = $xml->xpath("//category[@catid='1']/text");
$ids = ['TXT1', 'TXT2', 'TXT3'];
foreach($ids as $id){
echo $category[$id]; //I'm not quite sure how to do this bit.
}
?>
Any help is appreciated, thanks!
Here comes an example how to do it using the DOM extension together with XPATH: