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);Written by Maxim Krizhanovsky
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Did you know?
PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#