How can the use of DOM(XML) in PHP help with formatting XML output?
Using DOM(XML) in PHP can help with formatting XML output by providing a more structured and organized way to create and manipulate XML documents. The DOM extension in PHP allows you to easily create XML elements, set attributes, and add child nodes, making it simpler to generate well-formed XML output. Additionally, the DOM extension handles special characters and encoding automatically, ensuring that the XML output is properly formatted and valid.
// Create a new XML document
$doc = new DOMDocument();
// Create root element
$root = $doc->createElement('root');
$doc->appendChild($root);
// Create child elements
$child1 = $doc->createElement('child1', 'value1');
$root->appendChild($child1);
$child2 = $doc->createElement('child2', 'value2');
$root->appendChild($child2);
// Output the XML
echo $doc->saveXML();