#28797 [Csd->Bgs]: DOMNodeList->item works strangely

From: Date: Wed, 16 Jun 2004 10:46:34 +0000
Subject: #28797 [Csd->Bgs]: DOMNodeList->item works strangely
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-60986@lists.php.net to get a copy of this message
ID: 28797 Updated by: derick@php.net Reported By: benoit dot toutain at free dot fr -Status: Closed +Status: Bogus Bug Type: DOM XML related Operating System: Linux 2.6.3-4mdk PHP Version: 5.0.0RC3 New Comment: User error -> bogus Previous Comments: ------------------------------------------------------------------------ [2004-06-16 12:21:21] benoit dot toutain at free dot fr Thanks for the help and sorry to bother you. ------------------------------------------------------------------------ [2004-06-16 09:21:39] dharana at dharana dot net Nonono benoit. It's not a bug. I stumbled upon the same thing a few weeks ago but just take a close look: $nbChildNodes = $nodeList->length; echo "<br/>nbchildnodes = $nbChildNodes</br>\n"; for ($i = 0; $i < $nbChildNodes; $i++) { echo "i = $i<br/>\n"; $node = $nodeList->item($i); $nodeRoot->removeChild($node); } Whenever you do a removeChild $nodeList->length decrements by one so if you start with 8 elements in the nodelist and you remove the FIRST element the next iteration will go against child 3 because child 1 has been removed and child 2 is now on item(0). PHP is behaving correctly, it's your code. Try this: while ($nodeList->length > 0) { $node = $nodeList->item(0); $nodeRoot->removeChild($node) } ------------------------------------------------------------------------ [2004-06-15 17:38:45] benoit dot toutain at free dot fr Description: ------------ the item function of DOMNodeList class seems to not work correctly. It retreives only the first half part of NodeList members. Reproduce code: --------------- $doc = new DOMDocument; $nodeRoot = $doc->appendChild(new DOMElement("root")); for ( $i = 1; $i <= 10; $i++ ) { $newNode = $doc->importNode(new DOMElement("child")); $newNode = $nodeRoot->appendChild($newNode); $newNode->setAttribute("id",$i); } echo htmlentities($doc->saveXML()); $nodeList = $nodeRoot->childNodes; $nbChildNodes = $nodeList->length; echo "<br/>nbchildnodes = $nbChildNodes</br>\n"; for ($i = 0; $i < $nbChildNodes; $i++) { echo "i = $i<br/>\n"; $node = $nodeList->item($i); $nodeRoot->removeChild($node); } echo "<br/><br/>\n"; echo htmlentities($doc->saveXML()); Expected result: ---------------- <?xml version="1.0"?> <root><child id="1"/><child id="2"/><child id="3"/><child id="4"/><child id="5"/><child id="6"/><child id="7"/><child id="8"/><child id="9"/><child id="10"/></root> nbchildnodes = 10 i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 <?xml version="1.0"?> <root/> Actual result: -------------- <?xml version="1.0"?> <root><child id="1"/><child id="2"/><child id="3"/><child id="4"/><child id="5"/><child id="6"/><child id="7"/><child id="8"/><child id="9"/><child id="10"/></root> nbchildnodes = 10 i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 Warning: removeChild() expects parameter 1 to be DOMNode, null given in /home/apache/htdocs/test_dom/test.php on line 27 i = 6 Warning: removeChild() expects parameter 1 to be DOMNode, null given in /home/apache/htdocs/test_dom/test.php on line 27 i = 7 Warning: removeChild() expects parameter 1 to be DOMNode, null given in /home/apache/htdocs/test_dom/test.php on line 27 i = 8 Warning: removeChild() expects parameter 1 to be DOMNode, null given in /home/apache/htdocs/test_dom/test.php on line 27 i = 9 Warning: removeChild() expects parameter 1 to be DOMNode, null given in /home/apache/htdocs/test_dom/test.php on line 27 <?xml version="1.0"?> <root><child id="2"/><child id="4"/><child id="6"/><child id="8"/><child id="10"/></root> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=28797&edit=1

« previous php.bugs (#60986) next »