Traversing a Tree of XML Nodes Using On-Demand Functions
<?php $doc = new DomDocument("1.0"); $doc->loadXML( file_get_contents("data.xml") ); $root = $doc->firstChild; $pointer = $root; do { print $pointer->tagName."<br />\n"; } while ( $pointer = next_element( $pointer ) ); function next_element( DomNode $pointer ) { while ( $pointer = next_node( $pointer ) ) { if ( $pointer->nodeType == XML_ELEMENT_NODE ) { return $pointer; } } return false; } function next_node( DomNode $pointer ) { if ( $pointer->hasChildNodes() ) { return $pointer->firstChild ; } if ( $next = $pointer->nextSibling ) { return $next; } while( $pointer = $pointer->parentNode ) { if ( $next=$pointer->nextSibling ) { return $next; } } } ?>