Bug with DOMNode::insertBefore in 5.x?
| From: | Christoph Boget | Date: | Thu, 16 Feb 2012 15:44:45 +0000 |
| Subject: | Bug with DOMNode::insertBefore in 5.x? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-316618@lists.php.net to get a copy of this message | ||
I'm having problems using DOMNode::insertBefore(). In both php 5.3.8
and 5.2.7, an exception is thrown when I'm trying to copy (and import)
a node from one document in to another and inserting it in front of an
existing node thusly :
<?php
$a = '<rootnodea><foo>foo content</foo></rootnodea>';
$b = '<rootnodeb><bar>bar content</bar></rootnodeb>';
$DOMDocumentA = new DOMDocument();
$DOMDocumentA->loadXML($a);
$DOMDocumentB = new DOMDocument();
$DOMDocumentB->loadXML($b);
$foo = $DOMDocumentA->getElementsByTagName('foo')->item(0);
$bar = $DOMDocumentB->getElementsByTagName('bar')->item(0);
$importedFoo = $DOMDocumentB->importNode($foo);
$DOMDocumentB->insertBefore($importedFoo, $bar);
echo $DOMDocumentA->saveXML();
echo "\n\n=========================\n\n";
echo $DOMDocumentB->saveXML();
/* Output for PHP 5.3.8
Fatal error: Uncaught exception 'DOMException' with message 'Not
Found Error' in /Users/current_user/Desktop/domInsertBeforeTest.php:16
Stack trace:
#0 /Users/jim/Desktop/domInsertBeforeTest.php(16):
DOMNode->insertBefore(Object(DOMElement), Object(DOMElement))
#1 {main}
thrown in /Users/current_user/Desktop/domInsertBeforeTest.php on line 16
*/
?>
Taking the second domdocument out of the equation and doing it this
way so that we are working with nodes within the same document :
<?php
$a = '<rootnodea><foo>foo content</foo></rootnodea>';
$b = '<rootnodeb><bar>bar content</bar><baz>baz
content</baz></rootnodeb>';
$DOMDocumentA = new DOMDocument();
$DOMDocumentA->loadXML($a);
$DOMDocumentB = new DOMDocument();
$DOMDocumentB->loadXML($b);
$bar = $DOMDocumentB->getElementsByTagName('bar')->item(0);
$baz = $DOMDocumentB->getElementsByTagName('baz')->item(0);
// $importedFoo = $DOMDocumentB->importNode($foo);
$DOMDocumentB->insertBefore($baz, $bar);
echo $DOMDocumentA->saveXML();
echo "\n\n=========================\n\n";
echo $DOMDocumentB->saveXML();
/* Output for PHP 5.3.8
Fatal error: Uncaught exception 'DOMException' with message 'Not
Found Error' in /Users/current_user/Desktop/domInsertBeforeTest.php:16
Stack trace:
#0 /Users/jim/Desktop/domInsertBeforeTest.php(16):
DOMNode->insertBefore(Object(DOMElement), Object(DOMElement))
#1 {main}
thrown in /Users/current_user/Desktop/domInsertBeforeTest.php on line 16
*/
?>
Throws the same exception. Is there a problem with
DOMNode::insertBefore() in php5?
thnx,
Christoph