0
我有一個創建一個動態的方式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>
'$ dom-> formatOutput = true;' – cspray 2012-04-23 14:03:34
謝謝@CharlesSprayberry – Haritz 2012-04-23 14:06:24
小旁註;尤其是使用PHP腳本輸出內容時,請注意關閉'?>'。它可以在輸出結束時產生一個空白行,可以將其他腳本捆綁起來。 – 2012-04-30 21:45:10