-wehug
Last Updated: February 25, 2016
·
620
· darhazer

Iterative traversal of DOM

PHP has limited stack size when it comes to recursion. Because of this recursive parsing of DOM may not be suitable (you'll have to increase the stack size). Using queue-dequeue algorithm however is fast and easy:

// $xml is an instance of DOMDocument
// replace with 'body' if you don't need the header tags
$node = $xml->getElementsByTagName('html')->item(0); 
$level = 0;

$nodes = array($level => array($node));
do {
    while (!empty($nodes[$level])) {
       $node = array_shift($nodes[$level]); // FIFO
       // do whatherver you like with the node
       ...

       if ($node->childNodes && $node->childNodes->length) {
           $level++;
           $nodes[$level] = array();
           foreach ($node->childNodes as $childNode) {
                  array_push($nodes[$level], $childNode);
           }
       }
    }
    $level--;
} while ($level > 0);