How can XML output, such as DOMDocument::saveXML, be achieved when working with DOMDocument in PHP?

When working with DOMDocument in PHP, you can achieve XML output using the DOMDocument::saveXML method. This method allows you to save the XML representation of a DOMDocument object to a string, which can then be output or saved to a file.

<?php
// Create a new DOMDocument
$doc = new DOMDocument();

// Create some elements
$root = $doc->createElement('root');
$child = $doc->createElement('child', 'Hello World');

// Append the elements
$doc->appendChild($root);
$root->appendChild($child);

// Output the XML
echo $doc->saveXML();
?>