可能重复:
PHP XML 如何输出好的格式
我有以下以动态方式创建 XML 文档的 PHP DOM 代码:
<?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>