DOM/SimpleXML to be outputted nicely ;)
Posted: Wed May 14, 2008 7:00 am
Hello
What am I doing wrong in this? I want to create a function that outputs an xml-file, with multiple, easy to read lines and not create a file that has everything on one line, like file_put_contents("test.xml", $xml->asXML()); does. This does it too, even though I read this from php-manual comments which suggested that this is the way to go.
I believe that my error occurs on line 13 or 14, because rest of the original xml-file is saved fine.
What am I doing wrong in this? I want to create a function that outputs an xml-file, with multiple, easy to read lines and not create a file that has everything on one line, like file_put_contents("test.xml", $xml->asXML()); does. This does it too, even though I read this from php-manual comments which suggested that this is the way to go.
Code: Select all
<?php
function savexmlfile($xml) {
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$domnode = dom_import_simplexml($xml);
$domnode = $doc->importNode($domnode, true);
$domnode = $doc->appendChild($domnode);
file_put_contents("test.xml", $doc->saveXML());
}
unset ($xml);
$xml = simplexml_load_file("test.xml");
$category = $xml->addChild('category', '');
$category->addAttribute('name', utf8_encode('kablaa'));
$xml = savexmlfile($xml);
?>