第1列第2行的PHP导出为XML错误:文档末尾的额外内容

第1列第2行的PHP导出为XML错误:文档末尾的额外内容,php,xml,Php,Xml,当我在浏览器中看到页面源代码时,字段“id”丢失 代码如下,结果来自浏览器中的页面源代码 // Start XML file, create parent node $doc = domxml_new_doc("1.0"); $node = $doc->create_element("markers"); $parnode = $doc->append_child($node); // Select all the rows in the markers table $quer

当我在浏览器中看到页面源代码时,字段“id”丢失 代码如下,结果来自浏览器中的页面源代码

// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);



// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // Add to XML document node
  $node = $doc->create_element("marker");
  $newnode = $parnode->append_child($node);

  $newnode->set_attribute("id", $row['id']);
  $newnode->set_attribute("name", $row['name']);
  $newnode->set_attribute("address", $row['address']);
  $newnode->set_attribute("lat", $row['lat']);
  $newnode->set_attribute("lng", $row['lng']);
  $newnode->set_attribute("type", $row['type']);
}

$xmlfile = $doc->dump_mem();
echo $xmlfile;

?>
下面是Chrome页面源代码视图的输出

<markers><marker name="Love.Fish" lat="-33.861034" lng="151.171936" /><marker name="Young Henrys" lat="-33.898113" lng="151.174469" /><marker name="Hunter Gatherer" lat="-33.840282" lng="151.207474" /><marker name="The Potting Shed" lat="-33.910751" lng="151.194168" /><marker name="Nomad" lat="-33.879917" lng="151.210449" /><marker name="Three Blue Ducks" lat="-33.906357" lng="151.263763" /></markers>
一些样本数据

    $mysqli = new mysqli("localhost", "caico", "posxxxxt8", "ca888888co");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}



// Set the active MySQL database



// Select all the rows in the markers table
header("Content-type: text/xml");
$query = "SELECT * FROM markers WHERE 1";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        // Add to XML document node
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("id",$row['id']);
  $newnode->setAttribute("name",$row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("type", $row['type']);
    }

    /* free result set */
    $result->free();
}

请显示标记表的几行。是否有空id?表格标记有id列它是1 2 3谢谢我编辑了问题添加图像我捕获了表格标记的屏幕图像谢谢看起来像PHP 4 DOM的。你真的在某处使用它?似乎不仅缺少id属性,还缺少地址和类型…?您是否首先验证了$row中的相应值是否已实际设置?如果您实际将这些属性设置为“empty”或false,那么当整个内容以XML形式写入时,可能会忽略这些属性……首先,使用不同的MySQL API,如mysqli或PDO,如MySQL。其次,删除@符号,因为这会抑制任何错误。第三,尝试回显$row值并查看输出。在更改为mysqli后,这是工作。感谢所有帮助