note 80249 added to function.dom-domnodelist-item
| From: | vinyanov at poczta dot onet dot pl | Date: | Sun, 06 Jan 2008 06:12:45 +0000 |
| Subject: | note 80249 added to function.dom-domnodelist-item | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-134842@lists.php.net to get a copy of this message | ||
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. :)
----
Server IP: 82.160.94.10
Probable Submitter: 83.31.223.75
----
Manual Page -- http://www.php.net/manual/en/function.dom-domnodelist-item.php
Edit -- https://master.php.net/note/edit/80249
Del: integrated -- https://master.php.net/note/delete/80249/integrated
Del: useless -- https://master.php.net/note/delete/80249/useless
Del: bad code -- https://master.php.net/note/delete/80249/bad+code
Del: spam -- https://master.php.net/note/delete/80249/spam
Del: non-english -- https://master.php.net/note/delete/80249/non-english
Del: in docs -- https://master.php.net/note/delete/80249/in+docs
Del: other reasons-- https://master.php.net/note/delete/80249
Reject -- https://master.php.net/note/reject/80249
Search -- https://master.php.net/manage/user-notes.php