Possible Duplicate:
PHP xpath – find element with a value and also get elements before and after element
I have the following xml:
<Table>
<ID>100</ID>
<Name>Fridge</Name>
<Description>A cool refrigerator</Description>
</Table>
<Table>
<ID>100</ID>
<Name>Fridge</Name>
<Description>Latest Refrigerator</Description>
</Table>
<Table>
<ID>200</ID>
<Name>Fridge</Name>
<Description>Another refrigerator</Description>
</Table>
In the example above, I would like to get the child values of Name and Description for the nodes with ID=100. There are around 1000 Table nodes in the xml file.
How can i parse the entire xml and get the Name and Description values for only the nodes with ID equal to 100 ?
So far, i have tried the following code, which could not give what i wanted:
$source = 'Tables.xml';
$xmlstr = file_get_contents($source);
$sitemap = new SimpleXMLElement($xmlstr);
$sitemap = new SimpleXMLElement($source,null,true);
foreach($sitemap as $url) {
if($url->ID == '100')
{
echo 'Name: '.(string)$url->Name.', Description: '.(string)$url->Description.'<br>';
}
}
This should be pretty straightforward if you get all
Tabletags and loop over them: