DOM/SimpleXML to be outputted nicely ;)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nakkeli
Forum Newbie
Posts: 15
Joined: Thu Apr 10, 2008 1:18 am

DOM/SimpleXML to be outputted nicely ;)

Post by nakkeli »

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.

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);
?>
 
I believe that my error occurs on line 13 or 14, because rest of the original xml-file is saved fine.
Post Reply