function XMLTraversal ($curNode, $curDepth)
{
global $maxDepth;
if($curDepth > $maxDepth)
return;
$node = $curNode;
while($node != NULL)
{
if($node->nodeName == "#text")
{
$node = $node->nextSibling;
continue;
}
for( $i = 0 ; $i < $curDepth; $i++ )
{
echo " ";
}
echo "<".$node->nodeName;
if($node->hasAttributes())
{
$attrs = $node->attributes;
for ( $i = 0 ; $i < $attrs->length ; $i++ )
{
echo ' '.$attrs->item($i)->nodeName.'="'.$attrs->item($i)->nodeValue.'"';
}
}
echo ">";
echo ": ".$node->firstChild->nodeValue."
";
XMLTraversal($node->firstChild->nextSibling, $curDepth+1);
$node = $node->nextSibling;
}
}
?>
$maxDepth = 3;
if(isset($_GET['maxDepth']))
{
$maxDepth = $_GET['maxDepth'];
}
$domObj = new DOMDocument();
$domObj->load("books.xml");
$root = $domObj->documentElement;
?>