Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

php - Error on line 2 at column 1: Extra content at the end of the document

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.

Note: I retrieved the $lng and $lat from the query string and it works fine locally.

$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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

<mydocument>      
  <mycatch>
      ....
  </mycatch>
  <mycatch>
      ....
  </mycatch>
  ....
</mydocument>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...