[PHP] How to beautify XML code
If you manually write XML code without using a class like DOMDocument or SimpleXML, you’ll probably obtain a “one line file”, unless you intentionally insert break lines. As it’s a tedious work, there’s a useful PEAR class, XML_Beautifier, to do such a job. Here it is how to use it: first of all install it with pear by command line:
sudo pear install --alldeps --force XML_BeautifierThen by considering that you have all XML content in a string variable named
$xml, you can use this class in this way:
if (!$handle = fopen($file, 'w'))
die("Non si riesce creare il file ($file)");
else
{
$fmt = new XML_Beautifier();
$result = $fmt->formatString($xml);
$bytes = fwrite($handle, $result);
unset($fmt);
if($bytes !== false) echo ("FileOK")
else die("File KO");
}