An error requesting a hierarchy of errors with a DOMDocument in PHP means that you are trying to move the node to itself. Compare this to the snake in the following figure:

Similarly with your node. You move the node to yourself. This means that at the moment when you want to replace a person with a paragraph, the person is already a child of this paragraph.
The appendChild () method effectively moves a person from the DOM tree, it is no longer part of:
$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>
Eve is already gone. Its parentNode is already a paragraph.
So, instead, you first want to replace, and then add a 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 everything is all right.
hakre
source share