开发者

aligning the xml document

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>&开发者_如何学Golt;activity>jogging</activity></root>

how do i align it programmatically like this?

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


You can use this function

function pretty_xml($string) {
  $xml = DOMDocument::loadXML($string);
  $xml->formatOutput = true; 
  return $xml->saveXML();
}


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜