PHP DOM生成了XML break line

时间:2012-04-23 14:01:41

标签: php xml dom

  

可能重复:
  PHP XML how to output nice format

我有以下PHP DOM代码,它以动态方式创建XML文档:

<?php

// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");

for ($i=1;$i<=5;$i++)
{
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
}
// save and display tree
echo $dom->saveXML();
?>

PHP代码生成下一个XML代码:

<?xml version="1.0"?>
<toppings><item>pepperoni</item></toppings>
<toppings><item>pepperoni</item></toppings>
<toppings><item>pepperoni</item></toppings>
<toppings><item>pepperoni</item></toppings>
<toppings><item>pepperoni</item></toppings> 

我想知道要改变什么,以便XML看起来像这样:

<?xml version="1.0"?>
<toppings>
  <item>pepperoni</item>
</toppings>
<toppings>
  <item>pepperoni</item>
</toppings>
<toppings>
  <item>pepperoni</item>
</toppings>
<toppings>
  <item>pepperoni</item>
</toppings>
<toppings>
  <item>pepperoni</item>
</toppings> 

1 个答案:

答案 0 :(得分:3)

尝试$dom->formatOutput = true