XML Extra content at the end of the document
I'm getting this error:
error on line 4 at column 1: Extra content at the end of the document
with this code:
$this->load->database();
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
$query = $this->db->get('comboTable');
$query = $query->result_array();
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
foreach ($query as $row)
{
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['restaurantName']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
开发者_开发技巧 }
// End XML file
echo '</markers>';
Without seeing the text output itself, it would be impossible to know for sure, but there are a couple of things I noticed immediately:
- To debug this, send the header
header("Content-type: text");
. - parseToXML is an attempt to re-invent the wheel, use
htmlentities
. - (This and the following should be irrelevant, you should be using htmlentities) You are replacing
&
after replacing everything else with an entity. This means you're getting,&lt;
instead of<
. str_replace
takes arrays as parameters, you would be best using them.
After spending about 3 days on this very same problem, I have come to find the best and easiest way to get past this may be addding the folling headers to the code:
header("Content-type: text/xml");
header( 'Content-Disposition: attachment; filename='.$_SESSION['WorkTable'].'_contacts-'.$date.'.xml');
Add this to your code between this lines:
$query = $query->result_array();
//add it here <--
header("Content-type: text/xml");
精彩评论