How to display only necessary information from xml
- by fakson
I have xml table:
<?xml version="1.0" encoding="UTF-8"?>
<table>
<user>
<id>1</id>
<info>
<more>
<nick>John</nick>
<adress>blabla</adress>
</more>
<more>
<nick>Mike</nick>
<adress>blablabla</adress>
<tel>blablabla</tel>
</more>
</info>
</user>
<user>
<id>2</id>
<info>
<more>
<nick>Fake</nick>
<adress>blablabla</adress>
<tel>blablabla</tel>
</more>
</info>
</user>
</table>
And i need to read data from it. I made such parser.
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br /><br />";
echo '<hr />';
foreach ($xml->children() as $child1){
//echo $child1->getName() . ": " . $child1 . "<br />"; //el
foreach($child1->children() as $child2){
if ((string)$child2 == '2') {
echo "<strong>" .$child2 . "</strong><br />";
foreach($child2->children() as $child3){
echo "<strong>".$child3->getName()."</strong>:" . $child3 . "<br />";
foreach($child3->children() as $child4){
echo $child4->getName() . ": " . $child4 . "<br />";
}
}
}
}
echo '<hr />';
echo '<br />';
}
?>
I want to make search in xml file, for example get all information about user with id 2, i try to realize it with if function but i get only id.
What is wrong?. Any suggestions??