|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-07-18 13:14 UTC] giorgio dot liscio at email dot it
Description:
------------
hi, as summary says
here is the test code
<?php
$s = '<div><abc /><abc /><abc /><abc /></div>';
$a = new DOMDocument(); $a->loadXML($s);
$els = $a->getElementsByTagName("abc");
// uncomment those three lines to fix
//foreach($els as $b)
// $els2[]=$b;
//$els=$els2;
foreach($els as $el)
$el->parentNode->removeChild($el);
echo "<textarea rows=20 cols=50>";
echo "document element (<div>) should be empty:\n";
echo $a->saveXML();
?>
uncomment lines to see the expected behavior
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 10:00:01 2025 UTC |
Create a document <?php $document = new DOMDocument(); $document->formatOutput = true; $root = $document->appendChild($document->createElement('root')); for($i = 0; $i < 10; $i++) { $root->appendChild($document->createElement('child', $i)); } echo $document->saveXML(); ?> Result: <?xml version="1.0"?> <root> <child>0</child> <child>1</child> <child>2</child> <child>3</child> <child>4</child> <child>5</child> <child>6</child> <child>7</child> <child>8</child> <child>9</child> </root> When you alter elements in the DOMNodeList in a foreach loop, it may produce unexpected results <?php foreach($document->getElementsByTagName('child') as $element) { $root->removeChild($element); } echo $document->saveXML(); ?> Result: <?xml version="1.0"?> <root> <child>1</child> <child>3</child> <child>5</child> <child>7</child> <child>9</child> </root> First copy the elements from the DOMNodeList to an array with iterator_to_array <?php foreach(iterator_to_array($document->getElementsByTagName('child')) as $element) { $root->removeChild($element); } ?> Result: <?xml version="1.0"?> <root/>