php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28797 DOMNodeList->item works strangely
Submitted: 2004-06-15 17:38 UTC Modified: 2004-06-16 12:46 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: benoit dot toutain at free dot fr Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.0.0RC3 OS: Linux 2.6.3-4mdk
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: benoit dot toutain at free dot fr
New email:
PHP Version: OS:

 

 [2004-06-15 17:38 UTC] 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>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-16 09:21 UTC] 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-16 12:21 UTC] benoit dot toutain at free dot fr
Thanks for the help and sorry to bother you.
 [2004-06-16 12:46 UTC] derick@php.net
User error -> bogus
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 12:01:33 2025 UTC