I try the search code below but it is only showing the first child node. Is something missing from my code?
—-catalog.xml—-
<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
<Category>
<Name>CAT1</Name>
<Location>
<Room>Alpha</Room>
</Location>
<Location>
<Room>Bravo</Room>
</Location>
<Location>
<Room>Charlie</Room>
</Location>
</Category>
<Category>
<Name>CAT2</Name>
<Location>
<Room>Delta</Room>
</Location>
<Location>
<Room>Eagle</Room>
</Location>
<Location>
<Room>Falcon</Room>
</Location>
</Category>
</Catalog>
—-arr0.php—-
<?php
$catalog = simplexml_load_file("catalog.xml");
$category = $catalog->Category;
foreach($category->Name as $name)
{
$menu = (string)$name;
$i = 0;
if ($menu == "CAT1" )
{
echo $category->Location->Room[$i];
$i++;
}
else
{
echo "No result";
}
}
?>
———Output———-
Alpha
Bravo and Charlie is missing from display. Need someone advise where am I missing something?
Thanks
I think this is what you want
Remove the
break;statement if you can have more than one Category with the searched Name element.