Output the element's name and data for each child node in the XML file in PHP

Description

The following code shows how to output the element's name and data for each child node in the XML file.

Example


//from   w w  w.j av a 2  s .  c om
<?php
    $xml=simplexml_load_file("test.xml");
    echo $xml->getName() . "<br>";

    foreach($xml->children() as $child){
       echo $child->getName() . ": " . $child . "<br>";
    }
?>

The following code is for test.xml.


/*from  ww  w  .  j a  v  a  2s .com*/
    <?xml version="1.0" encoding="UTF-8"?>
    <note>
    <to>Work</to>
    <from>Home</from>
    <heading>Reminder</heading>
    <body>This is a message.</body>
    </note>

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser