开发者

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('<','&lt;',$htmlStr);     
        $xmlStr=str_replace('>','&gt;',$xmlStr); 
        $xmlStr=str_replace('"','&quot;',$xmlStr); 
        $xmlStr=str_replace("'",'&#39;',$xmlStr); 
        $xmlStr=str_replace("&",'&amp;',$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:

  1. To debug this, send the header header("Content-type: text");.
  2. parseToXML is an attempt to re-invent the wheel, use htmlentities.
  3. (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, &amp;lt; instead of &lt;.
  4. 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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜