|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 25 13:00:01 2025 UTC |
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) }