Here is a simple example for replacing a node:
Let's define our XML like so:
<?php
$xml = <<<XML
<?xml version="1.0"?>
<root>
<parent>
<child>bar</child>
<child>foo</child>
</parent>
</root>
XML;
?>
If we wanted to replace the entire <parent> node, we could do something like this:
<?php
$parent = new DomDocument;
$parent_node = $parent ->createElement('parent');
$parent_node->appendChild($parent->createElement('child', 'somevalue'));
$parent_node->appendChild($parent->createElement('child', 'anothervalue'));
$parent->appendChild($parent_node);
?>
Next, we need to locate the old node:
<?php
$dom = new DomDocument;
$dom->loadXML($xml);
$xpath = new DOMXpath($dom);
$nodelist = $xpath->query('/root/parent');
$oldnode = $nodelist->item(0);
?>
We then import and replace the new node:
<?php
$newnode = $dom->importNode($parent->documentElement, true);
$oldnode->parentNode->replaceChild($newnode, $oldnode);
echo $dom->saveXML();
?>
Our new node is successfully imported:
<?xml version="1.0"?>
<root>
<parent><child>somevalue</child><child>anothervalue</child></parent>
</root>