RSS | Archive | Random | E-mail

About

Hi, I'm Christian Castelli, a 28 years old italian programmer located in Pisa (Italy). Here I post small snippets of code which can be useful in my work.

Links

Codepuzzling main site
Development site
ByteStrike italian blog
Follow me on Twitter

My Life Style

while(passion) {
  try {
    myLife.run();
  }catch(LifeExceptions) {  
    stronger++;
    continue;
   }
}

Following

7 July 09

[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_Beautifier
Then 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");
}

Tags: php xml pear
Comments (View)
blog comments powered by Disqus
Themed by Hunson. Originally by Josh