Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 812
hunter Avatar asked Dec 28 '10 07:12

hunter


2 Answers

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>
like image 188
Jim Garrison Avatar answered Nov 02 '22 14:11

Jim Garrison


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.

like image 32
Tom E.C Avatar answered Nov 02 '22 12:11

Tom E.C