What are common errors when trying to output XML with PHP?

Common errors when trying to output XML with PHP include not setting the proper content-type header, not properly escaping special characters, and not correctly structuring the XML document. To solve these issues, make sure to set the content-type header to "application/xml", use functions like htmlspecialchars() to escape special characters, and ensure the XML document follows the correct structure.

<?php
// Set the content-type header
header('Content-type: application/xml');

// Create XML document
$xml = new DOMDocument('1.0', 'utf-8');

// Create root element
$root = $xml->createElement('root');
$xml->appendChild($root);

// Create child element
$child = $xml->createElement('child');
$root->appendChild($child);

// Add text content to child element
$child->appendChild($xml->createTextNode('Hello, World!'));

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