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:

snake eats itself

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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -