SimpleXML has its own SPL iterator. See http://www.php.net/~helly/php/ext/spl/classSimpleXMLIterator.html . But I guess that there are none for DOM nodes. By the way, two out of three implementations I found over the Net were not recursive, so I wrote my own. Here is the snippet:
<?php
class DOMNodeListIterator implements RecursiveIterator
{
private
$nodes,
$offset;
function __construct(DOMNodeList $nodes)
{
return $this -> nodes = $nodes;
}
function rewind()
{
return $this -> offset = 0;
}
function current()
{
return $this -> nodes -> item($this -> offset);
}
function key()
{
return $this -> current() -> nodeName;
}
function next()
{
return $this -> offset++;
}
function valid()
{
return $this -> offset < $this -> nodes -> length;
}
function hasChildren()
{
return isset($this -> current() -> childNodes -> length) && $this -> current() -> childNodes -> length > 0;
}
function getChildren()
{
return new self($this -> current() -> childNodes);
}
}
?>
Remember to use RecursiveIteratorIterator::SELF_FIRST flag when you create your iterator iterator.
<?php
$iterator = new DOMNodeListIterator($document -> childNodes);
$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
?>
Should work, has just few minutes though. :)