Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - 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>';  
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...