©
This document uses PHP Chinese website manual Release
(PHP 5, PHP 7)
$index
)The number of nodes in the list. The range of valid child node indices is 0 to length - 1 inclusive.
[#1] ignitedfirestarter at gmail dot com [2012-07-04 10:39:50]
If you want to recurse over a DOM then this might help:
<?php
class DOMNodeRecursiveIterator extends ArrayIterator implements RecursiveIterator {
public function __construct (DOMNodeList $node_list) {
$nodes = array();
foreach($node_list as $node) {
$nodes[] = $node;
}
parent::__construct($nodes);
}
public function getRecursiveIterator(){
return new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST);
}
public function hasChildren () {
return $this->current()->hasChildNodes();
}
public function getChildren () {
return new self($this->current()->childNodes);
}
}
?>
[#2] geompse at gmail dot com [2010-05-19 08:39:58]
Note that $length is calculated (php5.3.2).
Iterating over a large NodeList may be time expensive.
Prefer :
$nb = $nodelist->length;
for($pos=0; $pos<$nb; $pos++)
Than:
for($pos=0; $pos<$nodelist->length; $pos++)
I had a hard time figuring that out...
[#3] c dot 1 at smithies dot org [2008-08-27 15:26:49]
I doubt the accuracy of what saad105050 wrote below. In particular, in his example, he seems to assume that $element->getElementsByTagName() will return NULL if there are no matching nodes. This is not what happens; as per the documentation, a DOMNodeList is returned with the length property zero.
[#4] bobvandell at hotmail dot com [2008-08-26 09:12:01]
That's actually incorrect. You can use function results as objects. It makes building an API for your database very clean and neat. For example:
Our code:
$articles = Node::screate('tags', 123456)->assets('like:title:test')->articles;
We use the above code to get articles that are linked to assets that are linked to a specific tag in our database.
[#5] c dot 1 at smithies dot org [2008-05-23 05:43:49]
You can modify, and even delete, nodes from a DOMNodeList if you iterate backwards:
$els = $document->getElementsByTagName('input');
for ($i = $els->length; --$i >= 0; ) {
$el = $els->item($i);
switch ($el->getAttribute('name')) {
case 'MAX_FILE_SIZE' :
$el->parentNode->removeChild($el);
break;
case 'inputfile' :
$el->setAttribute('type', 'text');
//break;
}
}
[#6] drichter at muvicom dot de [2008-05-14 06:11:58]
Addition to my first note:
An traditional for-loop does not allow you to change the DOM-tree while looping - the effects are the nearly the same as with foreach. So you have to collect the nodes in an array and do the tree-altering stuff within a second loop (looping the array this time ...)
[#7] drichter at muvicom dot de [2008-05-14 04:56:18]
I have done some testing and have found 2 results:
(My System: Win XP with PHP 5.2.1)
1) Iteration with foreach does function correctly as "james dot j dot hackett at gmail dot com" writes, _if_ you only do readonly stuff with foreach or minor writings of some attributes.
2) foreach does not function, if you are doing some DOM-Operations while iterating. In my situation it was adding the iterated $node as an child to an new node:
$newNode = $dom->createElement('newNode') ;
foreach ($nodeList as $node) {
echo $node->nodeValue ;
$newNode->appendChild($node) ;
}
This only gives you the first element ...
I'm interpreting it as an confusing but correct behavior because of the changes within the $dom-object while appending the node at an additional place ...
So, if you want to do something like 2) use for, length and item() :)
[#8] james dot j dot hackett at gmail dot com [2008-05-08 09:47:49]
In Response to 'kassah at gmail'
You don't need to convert a DOMNodeList to an array in order iterate through it using 'foreach'. You can use foreach directly with the DOMNodeList.
$nodeList = $someDomDocument->getElementsbytagname('user');
foreach ($nodeList as $node) {
echo $node->nodeValue;
}
[#9] kassah at gmail dot com [2008-05-04 17:06:41]
// Converts a DOMNodeList to an Array that can be easily foreached
function dnl2array($domnodelist) {
$return = array();
for ($i = 0; $i < $domnodelist->length; ++$i) {
$return[] = $domnodelist->item($i);
}
return $return;
}
[#10] brack at wjp dot de [2008-04-21 02:35:26]
In PHP 5.2.5 (Windows) it is not possible to iterate correctly over the DOMNodeList object returned by DOMNode->childNodes using foreach. Instead I had to use the for loop in conjunction with the item() method of DOMNodeList for iterating over all child nodes correctly.
I don't know whether this is really a bug, but apparently it is.