3

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?

Community
  • 1
  • 1
Tower
  • 1,287
  • 3
  • 15
  • 25
  • Possible duplicate of [PHP simpleXML how to save the file in a formatted way?](http://stackoverflow.com/questions/798967/php-simplexml-how-to-save-the-file-in-a-formatted-way) – cweiske May 16 '17 at 11:54

3 Answers3

11

AFAIK simpleXML can't do it on its own.

However, DOMDocument can.

$dom = dom_import_simplexml($sxe)->ownerDocument;
$dom->formatOutput = TRUE;
$formatted = $dom->saveXML();
alex
  • 479,566
  • 201
  • 878
  • 984
  • 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 – Tower Apr 12 '12 at 23:50
  • @Tower Try calling `saveXML()` on `ownerDocument`. – alex Apr 12 '12 at 23:51
  • 2
    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. – Tower Apr 13 '12 at 00:04
  • @Tower How are you viewing it to confirm this? – alex Apr 13 '12 at 00:10
  • 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? – Tower Apr 13 '12 at 00:20
  • @Tower So you're using Notepad++ on Windows right? The newline characters are different between Windows and *nix machines. – alex Apr 13 '12 at 00:26
  • @Tower If you convert `\n` to `\n\r`, it should work on Windows. – alex Apr 13 '12 at 01:28
  • How do you mean convert? with a function? – Tower Apr 13 '12 at 01:32
  • @Tower A function is one method to do it. – alex Apr 13 '12 at 01:41
  • http://stackoverflow.com/questions/798967/php-simplexml-how-to-save-the-file-in-a-formatted-way actually works – cweiske May 16 '17 at 11:54
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"); 

alex
  • 479,566
  • 201
  • 878
  • 984
0

this code worked for me and it's also pretty clean:

header('Content-type: text/xml');

echo $xml->asXML();

where $xml is a SimpleXMLElement - this code will print the XML in the following way

<Attribute>
   <ChildAttribute>Value</ChildAttribute>
</Attribute>

This is taken from the official PHP documentation SimpleXMLElement::asXML

Hope this will help you!