希望的xml结构是
<root>
<line>
<point id=…… />
<point id=…… />
<line>
<point id=…… />
<point id=…… />代码如下
$dom = new DOMDocument("1.0");
$node = $dom->createElement("root");
$parnode = $dom->appendChild($node); for($i=0;$i<n;$i++)
{
$node_line = $dom->createElement("line");
$parnode_line = $node->appendChild($node_line);
while ($row = @mysql_fetch_assoc($result))
{
$node_point = $dom->createElement("point");
$newnode = $node_line->appendChild($node_point);
$newnode->setAttribute("id",$row['id']);
}
}
但总是报“XML 文档只能有一个顶层元素”,谁能告诉我是哪里错了?应该怎么写?
<root>
<line>
<point id=…… />
<point id=…… />
<line>
<point id=…… />
<point id=…… />代码如下
$dom = new DOMDocument("1.0");
$node = $dom->createElement("root");
$parnode = $dom->appendChild($node); for($i=0;$i<n;$i++)
{
$node_line = $dom->createElement("line");
$parnode_line = $node->appendChild($node_line);
while ($row = @mysql_fetch_assoc($result))
{
$node_point = $dom->createElement("point");
$newnode = $node_line->appendChild($node_point);
$newnode->setAttribute("id",$row['id']);
}
}
但总是报“XML 文档只能有一个顶层元素”,谁能告诉我是哪里错了?应该怎么写?
<line>
<point id=…… />
<point id=…… />
</line> //这里总要关闭吧?
<line>
<point id=…… />
<point id=…… />
$parnode_line = $node->appendChild($node_line);你是要给root加子节点。
就该是:
$node_line = $dom->createElement("line");
$parnode_line = $parnode->appendChild($node_line);$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;$root = $doc->createElement('book');
$root = $doc->appendChild($root);$title = $doc->createElement('title');
$title = $root->appendChild($title);$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);echo "Retrieving all the document:\n";
echo $doc->saveXML() . "\n";echo "Retrieving only the title part:\n";
echo $doc->saveXML($title);
http://www.111cn.net/search.php?keyword=php%C9%FA%B3%C9xml&Submit=%CE%B4%C0%B4%CB%D1%CB%F7&p=1
<line>
<point id=…… />
<point id=…… />
你忘记带 </Line>了
你参考下:http://www.hzl126.cn/?action=show&id=301