Tag Archives: SimpleXmlElement – save indented tree

SimpleXmlElement – save indented tree

As everyone should be aware, SimpleXmlElement class is not capable to preserve the indents in xml output.
To achieve that the DOMDocument class comes in handy.
[php]
//Format XML to save indented tree rather than one line and save
$dom = new DOMDocument(’1.0′);
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$dom->save(‘fileName.xml’);
[/php]