0

I have an XML file which is similar to following

<Navigation Name="Klengeräte">
<Market Type="Navigation"></Market>
<Navigation Name="Bodenpflege">

<Market Type="Navigation"></Market>

<Navigation Name="Wöchentches">
<Market Type="Navigation">
</Market>
<Navigation Name="Ultra One">
<personalinfo>
 <Name> testing</Name>
 <grade> A </grade>
<info> test information </info>
</personalinfo>
</Navigation>
</Navigation>
<personalinfo>
<Name> testing</Name>
<grade> A </grade>
<info> test information </info>
</personalinfo>
<personalinfo>
<Name> testing</Name>
<grade> A </grade>
<info> test information </info>
</personalinfo>
</Navigation>
</Navigation> 

and I want to read the personal information tags in the XML

If there is any case issues please ignore,I have tried in many ways but I did not find correct solution.

  • Have a read on the following links [http://stackoverflow.com/questions/699821/php-check-if-xml-node-exists-with-attribute][1] [http://stackoverflow.com/questions/3646153/check-if-xml-node-exists-in-php?rq=1][2] [http://stackoverflow.com/questions/6884142/how-to-check-if-element-exists-with-simplexml?rq=1][3] [1]: http://stackoverflow.com/questions/699821/php-check-if-xml-node-exists-with-attribute [2]: http://stackoverflow.com/questions/3646153/check-if-xml-node-exists-in-php?rq=1 [3]: http://stackoverflow.com/questions/6884142/how-to-check-if-element-exists-with-simplexml?rq – Deepak Aug 31 '12 at 03:16
  • Thanks for the links. I've already gone through this links, but i did not find any exact solution. if you find any please let me know. – rockingstar Aug 31 '12 at 08:21

2 Answers2

0

You can do it by using DOMDocument to get the 'personalinfo' tag and iterating with foreach and DOMNode .

$parser = new DOMDocument();
$parser->loadXML($xml);
$infos = $parser->getElementsByTagName('personalinfo');
foreach($infos as $info) { 
  foreach($info->childNodes as $infoChild) {
    echo $infoChild->nodeName . '====' . $infoChild->nodeValue . '<br>';
  }
}

This is not a perfect solution, but it should get you started in the right direction. See http://codepad.viper-7.com/f4u9FC for an implementation with your xml.

Community
  • 1
  • 1
Awemo
  • 875
  • 1
  • 12
  • 25
  • Can i get the solution using simple xml? here i'm using simple xml only. – rockingstar Aug 31 '12 at 14:24
  • I have no experience with SimpleXml. Take a look at the documentation, http://www.php.net/manual/en/simplexml.examples.php . – Awemo Aug 31 '12 at 14:55
  • Thanks for your help i got the solutions. We can get the information of all personal tags using `foreach($temp_xml->xpath('//PersonalInformation') as $product_xml){` – rockingstar Sep 02 '12 at 18:07
  • Glad I could be of help. Could you post your solution as an answer and mark it as the correct answer. That way anybody coming to this post will see the solution immediately. – Awemo Sep 03 '12 at 10:09
0

Thanks for your help i got the solutions.

We can get the information of all personal tags using

foreach($temp_xml->xpath('//PersonalInformation') as $product_xml){

  $name = $product_xml{'Name'};

  $grade = $product_xml{'grade'};

}

In this way we can read any value. It worked for me.