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

时间:2017-08-23 22:01:05

标签: 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
$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>

一些示例数据

https://i.stack.imgur.com/piC2L.png

1 个答案:

答案 0 :(得分:1)

    $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();
}