95

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.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
user61734
  • 2,813
  • 5
  • 25
  • 18
  • There's also the PEAR [XML_Beautifier](http://pear.php.net/package/XML_Beautifier) package. – karim79 Apr 28 '09 at 17:22
  • I know this is quite old question and you must have found solution. May be useful for others, have a look at it https://github.com/spatie/array-to-xml – Vaibhav Malushte Nov 19 '20 at 15:16

4 Answers4

155

You could use the DOMDocument class to reformat your code:

$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
echo $dom->saveXML();
Gumbo
  • 643,351
  • 109
  • 780
  • 844
32

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):

//Format XML to save indented tree rather than one line
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
//Echo XML - remove this and following line if echo not desired
echo $dom->saveXML();
//Save XML to file - remove this and following line if save not desired
$dom->save('fileName.xml');
Witman
  • 1,488
  • 2
  • 15
  • 19
19

Use dom_import_simplexml to convert to a DomElement. Then use its capacity to format output.

$dom = dom_import_simplexml($simple_xml)->ownerDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
echo $dom->saveXML();
Eric
  • 95,302
  • 53
  • 242
  • 374
troelskn
  • 115,121
  • 27
  • 131
  • 155
  • doesn't work. The function returns a DOMElement, not a DOMDocument – karka91 Aug 02 '12 at 13:18
  • Seems `documentElement` should be `ownerDocument`. Not sure if the api changed or this is just a typo. I've corrected it now. – troelskn Aug 02 '12 at 13:30
  • 3
    please note that this still doesn't work as the preserveWhiteSpace and formatOutput should be set _before_ importing the document to have any effect :) – karka91 Aug 02 '12 at 13:48
  • Interesting - right you are. Looks like Gumbo's answer will work though. – troelskn Aug 02 '12 at 18:27
2

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.

<?php
$xmlFile = 'filename.xml';
if( !file_exists($xmlFile) ) die('Missing file: ' . $xmlFile);
else
{
  $dom = new DOMDocument('1.0');
  $dom->preserveWhiteSpace = false;
  $dom->formatOutput = true;
  $dl = @$dom->load($xmlFile); // remove error control operator (@) to print any error message generated while loading.
  if ( !$dl ) die('Error while parsing the document: ' . $xmlFile);
  echo $dom->save($xmlFile);
}
?>
Community
  • 1
  • 1
quantme
  • 3,609
  • 4
  • 34
  • 49