php 第 1 列第 2 行的错误:文档末尾的额外内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4544272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error on line 2 at column 1: Extra content at the end of the document
提问by hunter
When I use the below code and parse the xml locally it works fine but when upload the same script at the server it shows error.
当我使用下面的代码并在本地解析 xml 时,它工作正常,但是当在服务器上上传相同的脚本时,它显示错误。
Note: I retrieved the $lng
and $lat
from the query string and it works fine locally.
注意:我从查询字符串中检索了$lng
和$lat
并且它在本地工作正常。
$lng=$_GET['lng'];
$lat=$_GET['lat'];
$conn=new LoginSystem();
$conn->connect();
$dom = new DOMDocument("1.0");
$query="select catch_id,catch_details,image from mycatch where longitude='$lng' AND latitude='$lat'";
$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)){
$node = $dom->createElement("mycatch");
$node = $dom->appendChild($node);
foreach ($row as $fieldname => $fieldvalue) {
$child = $dom->createElement($fieldname);
$child = $node->appendChild($child);
$value = $dom->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
}
$conn->disconnect();
$xml_string = $dom->saveXML();
echo $xml_string;
On the server it throws this error. And the document is also empty.....
在服务器上,它会引发此错误。而且文档也是空的.....
This page contains the following errors:
error on line 2 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error.
此页面包含以下错误:
第 1 列第 2 行的错误:文档末尾的额外内容 以下是直到第一个错误为止的页面呈现。
回答by Jim Garrison
I think you are creating a document that looks like this:
我认为您正在创建一个如下所示的文档:
<mycatch>
....
</mycatch>
<mycatch>
....
</mycatch>
This is not a valid XML document as it has more than one root element. You must have a single top-level element, as in
这不是一个有效的 XML 文档,因为它有多个根元素。您必须有一个顶级元素,如
<mydocument>
<mycatch>
....
</mycatch>
<mycatch>
....
</mycatch>
....
</mydocument>
回答by Tom E.C
The problem is database connection string, one of your MySQL database connection function parameter is not correct ,so there is an error message in the browser output, Just right click output webpage and view html source code you will see error line followed by correct XML output data(file). I had same problem and the above solution worked perfectly.
问题是数据库连接字符串,你的MySQL数据库连接函数参数之一不正确,所以浏览器输出中有错误信息,只需右键单击输出网页并查看html源代码,您将看到错误行,然后是正确的XML输出数据(文件)。我遇到了同样的问题,上述解决方案效果很好。
回答by Brett Kail
On each loop of the result set, you're appending a new root element to the document, creating an XML document like this:
在结果集的每个循环中,您将一个新的根元素附加到文档中,创建一个像这样的 XML 文档:
<?xml version="1.0"?>
<mycatch>...</mycatch>
<mycatch>...</mycatch>
...
An XML document can only have one root element, which is why the error is stating there is "extra content". Create a single root element and add all the mycatch elements to that:
一个 XML 文档只能有一个根元素,这就是错误指出存在“额外内容”的原因。创建单个根元素并将所有 mycatch 元素添加到其中:
$root = $dom->createElement("root");
$dom->appendChild($root);
// ...
while ($row = @mysql_fetch_assoc($result)){
$node = $dom->createElement("mycatch");
$root->appendChild($node);
回答by John Erck
You might have output (maybe error/debug output) that precedes your call to
您可能在调用之前有输出(可能是错误/调试输出)
header("Content-type: text/xml");
Therefore, the content being delivered to the browser is not "xml"... that's what the error message is trying to tell you (at least that was the case for me and I had the same error message as you've described).
因此,传递给浏览器的内容不是“xml”......这就是错误消息试图告诉你的(至少对我来说是这种情况,我有与你描述的相同的错误消息)。