|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-08-14 12:04 UTC] rrichards@php.net
[2007-08-14 12:32 UTC] TomTrnka at seznam dot cz
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 02:00:01 2025 UTC |
Description: ------------ When a node tree is copied from one DOMDocument to another using recursive importNode on the tree root and then copying the childNodes in a loop (doesn't matter whether for or foreach) using e.g. appendChild, the loop is no more repeated after copying the first element, that means, other child elements do not get copied. Additionally, the source childNodes->length gets decremented. The opposite approach - iterating through the source elements and importing&appending one at a time works flawlessly. On commenting out the appendChild call the iteration goes normally, too. Reproduce code: --------------- <?php error_reporting(E_ALL | E_STRICT); $srcdoc = new DOMDocument("1.0", "UTF-8"); $src = $srcdoc->appendChild($srcdoc->createElement("srcroot")); $src->appendChild($srcdoc->createElement("elem1", "1")); $src->appendChild($srcdoc->createElement("elem2", "2")); echo $srcdoc->saveXML(); $dstdoc = new DOMDocument("1.0", "UTF-8"); $dst = $dstdoc->appendChild($dstdoc->createElement("dstroot")); $tmp = $dstdoc->importNode($src, TRUE); echo $tmp->childNodes->length; foreach ($tmp->childNodes as $child) { echo "."; $dst->appendChild($child); } echo "\n"; echo $dstdoc->saveXML(); ?> Expected result: ---------------- The output should look like this (both elements copied): <?xml version="1.0" encoding="UTF-8"?> <srcroot><elem1>1</elem1><elem2>2</elem2></srcroot> 2.. <?xml version="1.0" encoding="UTF-8"?> <dstroot><elem1>1</elem1><elem2>2</elem2></dstroot> Actual result: -------------- The output looks like this: <?xml version="1.0" encoding="UTF-8"?> <srcroot><elem1>1</elem1><elem2>2</elem2></srcroot> 2. <?xml version="1.0" encoding="UTF-8"?> <dstroot><elem1>1</elem1></dstroot> That means the elem2 did not get copied. There is also only one dot at the "2." row, meaning the second iteration of the loop hasn't even started.