Prettifying/Formatting output in SimpleXML

11,698

Solution 1

AFAIK simpleXML can't do it on its own.

However, DOMDocument can.

$dom = dom_import_simplexml($sxe)->ownerDocument;
$dom->formatOutput = TRUE;
$formatted = $dom->saveXML();

Solution 2

i think above accepted answer from stackoverflow authority did not solved above question. Ref : [ I tried your answer and i received a Fatal error: Call to undefined method DOMElement::saveXML() on the line where the $formatted = $dom->saveXML();

$simplexml = simplexml_load_file("links.xml");
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simplexml->asXML());
$xml = new SimpleXMLElement($dom->saveXML());

$person = $xml->addChild("link");
$person->addChild("title", "Q: ".$x_title);
$person->addChild("url", "questions_layout.php#Question$id");
$xml->saveXML("links.xml"); 

Share:
11,698
Tower
Author by

Tower

Updated on July 16, 2022

Comments

  • Tower
    Tower almost 2 years

    I have this simplexml script i'm using to post data entered from a form.

    $xml = simplexml_load_file("links.xml");
    
    $sxe = new SimpleXMLElement($xml->asXML()); 
    
    $person = $sxe->addChild("link");
    $person->addChild("title", "Q: ".$x_title);
    $person->addChild("url", "questions_layout.php#Question$id");
    
    $sxe->asXML("links.xml"); 
    

    and when it comes out it looks like this on one line:

    <link><title>Alabama State</title><url>questions_layout.php#Question37</url></link><link><title>Michigan State</title><url>questions_layout.php#Question37</url></link></pages>
    

    But i've tried the method found HERE and THIS AS WELL but neither format the XML correctly in the lines as they should be like

    <link>
    <title></title>
    <url></url>
    </link>
    

    In the first reference link I even changed loadXML to load, because loadXML expects a string as XML. Can somebody help me find a solution to this please?

  • Tower
    Tower about 12 years
    I tried your answer and i received a Fatal error: Call to undefined method DOMElement::saveXML() on the line where the `$formatted = $dom->saveXML(); happens
  • user3167101
    user3167101 about 12 years
    @Tower Try calling saveXML() on ownerDocument.
  • Tower
    Tower about 12 years
    Changed it to $dom = dom_import_simplexml($sxe)->ownerDocument; and though the error isn't being printed anymore, the XML is still not being formatted and appears all on one line.
  • user3167101
    user3167101 about 12 years
    @Tower How are you viewing it to confirm this?
  • Tower
    Tower about 12 years
    I entered data into the form again, submit it, let the PHP do it's job and then take a look at the XML file in Notepad++, but now that i see it, it only appears this way in Notepad++, but in IE or Firefox, it appears organized. Still should i be concerned about making it format properly in the editor as well?
  • user3167101
    user3167101 about 12 years
    @Tower So you're using Notepad++ on Windows right? The newline characters are different between Windows and *nix machines.
  • Tower
    Tower about 12 years
    Ok i see. Thanks for the answer.
  • user3167101
    user3167101 about 12 years
    @Tower If you convert \n to \n\r, it should work on Windows.
  • Tower
    Tower about 12 years
    How do you mean convert? with a function?
  • user3167101
    user3167101 about 12 years
    @Tower A function is one method to do it.
  • cweiske
    cweiske almost 7 years