I have the sample xml file below:
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="1" name="Car">
<category id="12" name="Electrical">
<category id="18" name="On-Road">
<category id="127" name="Carisma"></category>
<category id="128" name="HPI - Maverick"></category>
<category id="452" name="HSP"></category>
<category id="130" name="Other"></category>
</category>
<category id="16" name="Off-Road">
<category id="132" name="HPI - Maverick"></category>
<category id="225" name="Carisma"></category>
<category id="315" name="HSP"></category>
<category id="420" name="Other"></category>
</category>
</category>
</category>
</categories>
What I need is to all the parent nodes of any child node whit a specifi id value.
For example if the id value is 128, then it will echo: Car/Electrical/On-Road/HPI - Maverick
if the id value is 12, then it will echo Car/Electrical
and goes on...
I have managed to get the node with the specific id value (below) but I can't figure out how to get the parents chain from the selected node.
<?php
$file = "http://www.localhost.com/categories.xml";
$microline = new SimpleXMLElement($file, null, true);
foreach($microline as $cat){
if ($cat->children['id'] = $catid) {
$current_cat_name = $cat->xpath("//*[@id=$catid]/@name")[0];
echo $current_cat_name;
}
}
unset($microline);
?>