createElement("root"); $doc->appendChild($root); $child1 = $doc->createElement("child1", "value1"); $root->appendChild($child1); $child2 = $doc->createElement("child2", "value2"); $root->appendChild($child2); echo $doc->saveXML(); ?>
In this example, we first create an XML document as a string and load it into a DOMDocument object. We then add a new child element to the root element and print the updated XML output. Both of these examples use the PHP standard library for DOM manipulation, without any external package dependencies.value1 XML; $doc = new DOMDocument(); $doc->loadXML($xml); $root = $doc->documentElement; $child2 = $doc->createElement("child2", "value2"); $root->appendChild($child2); echo $doc->saveXML(); ?>