Source Code : Traversing a Tree of XML Nodes Using Recursion

PHP Is Open Source Programming Language You Can Download and use It, You required xampp server and wamp server . You download From xampp server from https://www.apachefriends.org/download.html. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://php.net/. Click Here or search from google with Libraries Name you get jar file related it and also http://php.net/ is a official site

Traversing a Tree of XML Nodes Using Recursion

 
<?php

$doc = new DomDocument("1.0");
$doc->loadXML( file_get_contents("data.xml") );
$root = $doc->firstChild;
traverse( $root );

function traverse( DomNode $node, $level=0 ){
  handle_node( $node, $level );
 if ( $node->hasChildNodes() ) {
   $children = $node->childNodes;
   foreach( $children as $kid ) {
     if ( $kid->nodeType == XML_ELEMENT_NODE ) {
       traverse( $kid, $level+1 );
     }
   }
 }
}

function handle_node( DomNode $node, $level ) {
  for ( $x=0; $x<$level; $x++ ) {
    print " ";
  }
  if ( $node->nodeType == XML_ELEMENT_NODE ) {
    print $node->tagName."<br />
";
  }
}
?>
  
  

Thank with us