XML with PHP "echo"显示错误“文档末尾有额外内容”


XML with PHP "echo" getting error "Extra content at the end of the document"

我得到一个错误,我认为是非常具体的。

这个PHP存储在我的域中。它使用mysql数据库访问服务器,并使用它的表信息生成带有标记的XML,这将随后在谷歌地图中使用。

实际上它正在工作,因为它正在检索标记,但是这个错误没有消失,我不知道是什么原因造成的。我对所有php和android编程都很陌生。如果有人能给我简单解释一下,我会很感激的。

错误是:

This page contains the following errors:
error on line 3 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.

然后它呈现我期望的结果。我想解决并理解为什么会发生这种情况。

这就是我的php代码。我从教程(https://developers.google.com/maps/articles/phpsqlajax_v3):

<?php
require("db_config.php");
function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 
// Opens a connection to a MySQL server
$connection=mysql_connect ($mysql_host, $mysql_user, $mysql_password);
if (!$connection) {  die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($mysql_database, $connection);
if (!$db_selected) {
    die ('Can''t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM Estac WHERE 1";
$result = mysql_query($query);
if (!$result) {
        die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker>';
  echo 'Name="' . parseToXML($row['Name']) . '" ';
  echo 'Address="' . parseToXML($row['Address']) . '" ';
  echo 'Tel="' . parseToXML($row['Tel']) . '" ';
  echo 'Lat="' . $row['Lat'] . '" ';
  echo 'Lng="' . $row['Lng'] . '" ';
  echo 'Price="' . $row['Price'] . '" ';
  echo '</marker>';
}
// End XML file
echo '</markers>';
?>

非常感谢!

右键单击输出错误网页并查看源代码,您将看到PHP文件中正确的错误消息和行号。您将在几秒钟内解决该错误。

"文档末尾的额外内容"

我想解决并理解为什么会发生这种情况。

为什么会发生这种情况?简言之,这是一个无效的XML。请看下面的例子:

<xml>
</xml>
This here is extra content at the end of the document
如您所见,通常没有人会创建这样一个XML文件。在您的例子中,这种情况的发生是因为程序员在编写输出XML的函数时过于聪明,而这些函数已经存在。他们只是忘记了正确地输出xml,然后他们就完蛋了:
$xml = new SimpleXMLElement('<markers/>');
foreach ($databaseResult as $row) 
{
    $marker = $xml->addChild('marker');
    foreach ($row as $key => $value) 
    {
        $marker[$key] = $value;
    }
}
header("Content-type: text/xml");
$xml->asXML('php://output');

这个例子使用了SimpleXML库。如果您的数据库结果非常大,而您希望以流方式传输数据,则可以查看XMLWriter库:

$writer = new XMLWriter();
$writer->openUri('php://output');
$writer->startDocument();
$writer->startElement('markers');
header("Content-type: text/xml");
foreach ($databaseResult as $row)
{
    $writer->writeRaw("'n  ");
    $writer->flush();
    $writer->startElement('marker');
    foreach ($row as $key => $value)
    {
        $writer->writeAttribute($key, $value);
    }
    $writer->endElement();
}
$writer->writeRaw("'n");
$writer->endElement();
$writer->flush();

我也有同样的问题,把' '放在数据库周围,它工作了。