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

☆樱花仙子☆ 提交于 2019-11-26 23:45:59

问题


I'm getting an error that I believe to be very specific.

This PHP is stored in my domain. It access a server with a mysql database and uses it's table information to generate an XML with markers, that will afterwards be used in Google Map.

Actually it is working because it's retrieving the markers, but this error doesn't go away, and I don't know what's causing it. I'm very new to all the php and android programming. If someone could clarify this for me with easy explanation, I would be greatful.

The error is:

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.

And then it renders my expected result. I would like to solve and understand why this happens.

There you go my php code. I got from the tutorial (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>';

?>

Thanks a lot!


回答1:


Just Right click your output error webpage and VIEW SOURCE CODE you will see the correct error message and line number from your PHP file. You will solve that error in few seconds.




回答2:


“Extra content at the end of the document”

I would like to solve and understand why this happens.

Why does this happen? This is in short an invalid XML. See the following example:

<xml>
</xml>
This here is extra content at the end of the document

As you can see, nobody would normally create such an XML file. In your case this happens because of a common accident of those programmers who outsmart themselves writing functions to output XML while those functions already exist. They just then forgot to properly output xml and then they are screwed:

$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');

This example is using the SimpleXML library. If your database result is very large and you want to stream the data instead, you can take a look at the XMLWriter library:

$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();

See it in action.




回答3:


I had same problem and put ' ' around database and it worked.



来源:https://stackoverflow.com/questions/16026944/xml-with-php-echo-getting-error-extra-content-at-the-end-of-the-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!