Return the name of the XML element and the children in PHP

Description

The following code shows how to return the name of the XML element and the children.

Example


  // ww  w  .  j  a  va  2s .c  om
<?php
   $xml=<<<XML
   <cars>
     <car id="1">XML</car>
     <car id="2">CSS</car>
     <car id="3">HTML</car> 
   </cars>
XML;
   
   $sxe=new SimpleXMLElement($xml);
   echo $sxe->getName() . "<br>";
   foreach ($sxe->children() as $child){
      echo $child->getName() . "<br>";
   }
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser