With DOM you use methods of the document object to create the node and methods of the parent node to insert/add it to the hierarchy.
DOMDocument has create* methods for the different node types (element, text, cdata section, comment, ...). The parent nodes (element, document, fragment) have methods like appendChild and insertBefore to add/remove them.
Xpath can be used to fetch nodes from the DOM.
$document = new DOMDocument;
$document->preserveWhiteSpace = FALSE;
$document->loadXML($xmlString);
$xpath = new DOMXpath($document);
// fetch the first Data element inside the Report document element
foreach ($xpath->evaluate('/Report/Data[1]') as $data) {
// create the name element and append it
$name = $data->appendChild($document->createElement('name'));
// create a node for the text content and append it
$name->appendChild($document->createTextNode('Vivian'));
}
$document->formatOutput = TRUE;
echo $document->saveXML();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<Report>
<Data>
<id>87236</id>
<purchase>3</purchase>
<address>XXXXXXXX</address>
<name>Vivian</name>
</Data>
</Report>