php - Uncaught exception 'DOMException' with message 'Hierarchy Request Error' -
i'm getting error while replacing or adding child node.
required :
i want change to..
<?xml version="1.0"?> <contacts> <person>adam</person> <person>eva</person> <person>john</person> <person>thomas</person> </contacts>
like
<?xml version="1.0"?> <contacts> <person>adam</person> <p> <person>eva</person> </p> <person>john</person> <person>thomas</person> </contacts>
error is
fatal error: uncaught exception 'domexception' message 'hierarchy request error'
my code is
function changetagname($changeble) { ($index = 0; $index < count($changeble); $index++) { $new = $xmldoc->createelement("p"); $new ->setattribute("channel", "wp.com"); $new ->appendchild($changeble[$index]); $old = $changeble[$index]; $result = $old->parentnode->replacechild($new , $old); } }
the error hierarchy request error domdocument in php means trying move node itself. compare snake in following picture:
similar node. move node itself. means, moment want replace person paragraph, person children of paragraph.
the appendchild() method moves person out of dom tree, not part longer:
$para = $doc->createelement("p"); $para->setattribute('attr', 'value'); $para->appendchild($person); <?xml version="1.0"?> <contacts> <person>adam</person> <person>john</person> <person>thomas</person> </contacts>
eva gone. parentnode paragraph already.
so instead first want replace , append child:
$para = $doc->createelement("p"); $para->setattribute('attr', 'value'); $person = $person->parentnode->replacechild($para, $person); $para->appendchild($person); <?xml version="1.0"?> <contacts> <person>adam</person> <p attr="value"><person>eva</person></p> <person>john</person> <person>thomas</person> </contacts>
now fine.
Comments
Post a Comment