I'm trying add some data to an existing XML file using PHP's SimpleXML. The problem is it adds all the data in a single line:
<name>blah</name><class>blah</class><area>blah</area> ...
And so on. All in a single line. How to introduce line breaks?
How do I make it like this?
<name>blah</name>
<class>blah</class>
<area>blah</area>
I am using asXML()
function.
Thanks.
Use
dom_import_simplexml
to convert to a DomElement. Then use its capacity to format output.You could use the DOMDocument class to reformat your code:
Gumbo's solution does the trick. You can do work with simpleXml above and then add this at the end to echo and/or save it with formatting.
Code below echos it and saves it to a file (see comments in code and remove whatever you don't want):
As Gumbo and Witman answered; loading and saving an XML document from an existing file (we're a lot of newbies around here) with DOMDocument::load and DOMDocument::save.