How can DOMDocument be used as an alternative to SimpleXML for XML manipulation in PHP?

When SimpleXML doesn't provide enough flexibility or functionality for XML manipulation in PHP, DOMDocument can be used as an alternative. DOMDocument allows for more advanced manipulation of XML documents, including creating, modifying, and deleting nodes, attributes, and elements.

// Create a new DOMDocument
$doc = new DOMDocument();

// Load the XML file
$doc->load('example.xml');

// Get the root element
$root = $doc->documentElement;

// Create a new element
$newElement = $doc->createElement('newElement');
$newElementText = $doc->createTextNode('This is a new element');
$newElement->appendChild($newElementText);

// Append the new element to the root element
$root->appendChild($newElement);

// Save the modified XML
$doc->save('example.xml');