希望的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 文档只能有一个顶层元素”,谁能告诉我是哪里错了?应该怎么写?

解决方案 »

  1.   

    看看这里http://topic.csdn.net/t/20031230/21/2619527.html
      

  2.   

    <root>
      <line>
      <point id=…… />
      <point id=…… />
      </line> //这里总要关闭吧?
      <line>
      <point id=…… />
      <point id=…… />
      

  3.   

    $node_line = $dom->createElement("line");  
    $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);
      

  4.   

    到这里看看N多生成xml 文档
    http://www.111cn.net/search.php?keyword=php%C9%FA%B3%C9xml&Submit=%CE%B4%C0%B4%CB%D1%CB%F7&p=1
      

  5.   

    <root>
      <line>
      <point id=…… />
      <point id=…… />
    你忘记带 </Line>了
      

  6.   

    你这个XML结构有错误。我刚在我博客里写了个操作XML的文章。
    你参考下:http://www.hzl126.cn/?action=show&id=301