views:

42

answers:

3

i'm inserting the data into an xml file using php domdocument. however when i open the xml file, the data is displayed in a single line:

<?xml version="1.0"?>
<root><activity>swimming</activity><activity>jogging</activity></root>

how do i align it programmatically like this?

<?xml version="1.0"?>
<root>
  <activity>swimming</activity>
  <activity>jogging</activity>
</root>
A: 

If you want to display it in easily readable form, http://gdatatips.blogspot.com/2008/11/xml-php-pretty-printer.html

Mark Baker
+6  A: 

You can use this function

function pretty_xml($string) {
  $xml = DOMDocument::loadXML($string);
  $xml->formatOutput = true; 
  return $xml->saveXML();
}
jcubic
where do i call this function?
fuz3d
Put this before you send your xml to browser. If you already use $xml->saveXML() (from domdocument) to output xml then just put $your_document->formatOutput = true; before sending xml to browser.
jcubic
The important feature here, as jcubic's last comment suggests, is the DOMDocument '$formatOutput' property, which can be set at any time after the object is created (i.e., you don't necessarily have to use it inside a user function. See the documentation for DOMDocument.)
GZipp
thank you. it worked.
fuz3d
+1  A: 

Use newlines and tabs with "\n" and "\t" respectively, in double quotes in your PHP code.

Tim Chaves
tried this. when i click enter to insert the data, the page goes blank instead of displaying the data. and in the xml file, while it created the nodes, they are empty.
fuz3d