How to display only necessary information from xml

Posted by fakson on Stack Overflow See other posts from Stack Overflow or by fakson
Published on 2010-03-26T13:31:47Z Indexed on 2010/03/26 14:03 UTC
Read the original article Hit count: 483

Filed under:
|

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??

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml