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

Description

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

Example


//from  w w w  . ja  v a  2s .com
<?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 av  a 2s . co m*/
    <?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