我想创建一个应用程序,用于搜索具有相应区域邮政编码的属性,并在谷歌地图上找到它们。我正在测试谷歌的教程,以使用 PHP/MySql 来使用 map ,但陷入了 XML 中。 它给了我这个错误:- 该页面包含以下错误:
第 1 列第 2 行出现错误:文档末尾有多余内容 下面是出现第一个错误之前的页面呈现。
这是代码:
<?php
$username="root";
$password="";
$database="test";
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers";
$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 = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", $row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("distance", $row['distance']);
}
$smd=$dom->saveXML();
echo $smd;
?>
最佳答案
大多数情况下,当没有看到 XML 根元素或不唯一时会导致此错误!
在你的情况下:我猜你的 XML 创建因 die() 失败,并且解析此 PHP 输出的外部脚本不会将其识别为有效的 XML?!
StasGrin 上面的评论可能是一个很好的观点: 最后把前面的空格去掉! 两者都会呈现空白,但这也会导致 PHP 错误/警告:
Warning: Cannot modify header information - headers already sent by (output started at …) in …
关于php - XML 文档末尾的额外内容(使用 PHP SimpleXML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12328874/