0

重複の可能性:
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> 
4

1 に答える 1

6

試してみてください$dom->formatOutput = true

于 2012-04-23T14:03:47.313 に答える