Outputting formatted XML using PHP
For an interpreter it doesn’t matter how much whitespace there is in an XML document and often you find that machines strip it all out to reduce the amount of bandwidth used. Often at the receiving end you want to show the output in a log file or page and need it to be formatted so that it is easily readable by humans.
The following code snippet will format the XML for you:
$dom = new DOMDocument(); $dom->loadXML($xml); $dom->formatOutput = true; $pretty_xml = $dom->saveXML();
In this example the $xml variable is a plain-text XML string that needs to be ‘prettified’.
I hope this little snippet has helped you. It did for me!