SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML Error: Invalid Character in content (only in IE)

    Hello all,

    I'm generating XML content from a MySQL database using PHP, but occasionally get the XML error: Invalid Character in content.

    The weird thing about the error is that it's only in IE6 (Firefox doesn't get the error), and it doesn't always occur! In fact, I can usually refresh the page and the error would disappear (even though I'm still getting the same content from the database).

    The relevant code is given below:

    PHP Code:
    $result mysql_query('select * from '.$table);
     
    header('Content-type: text/xml; charset=windows-1256');
     echo 
    "<table>";
    // rows while-loop: Goes through all the rows returned from the database
    while($row =  @mysql_fetch_array($result)){
     echo 
    "<row>";
     
    $i 0;
     
    // column while-loop: Determines the number of columns in the table, and gets the data for each column
     
    while($i mysql_num_fields($result)) {
       
    //  echo "Information for column $i:<br />\n";
      
    $meta mysql_fetch_field($result$i);
      if (!
    $meta) {
       echo 
    "<error>No information available</error>";
      } 
    // <![CDATA[". htmlspecialchars($row[$i]) ."]]>
      
    echo "<$meta->name>"htmlspecialchars($row[$i]) ."</$meta->name>";
      
    $i++;
     } 
    // end columns while-loop
     
    echo "</row>";
    // end rows while-loop
     
    echo "</table>"
    Please Note:

    * I used htmlspecialchars() when displaying the content, so - technically - there shouldn't be any dubious HTML/XML characters in the content (e.g. &).

    * I defined the encoding as windows-1256 because I have some Arabic characters in the database.

    * Some of the data contains French characters, like .

    Any help will be highly appreciated!

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sometimes it's better to enclose your object properties with curly braces when they're inside of a quoted string:
    Your code
    PHP Code:
    echo "<$meta->name>"htmlspecialchars($row[$i]) ."</$meta->name>"
    Here's the suggestion:
    PHP Code:
    echo "<{$meta->name}>"htmlspecialchars($row[$i]) ."</{$meta->name}>"
    I dunno if this will help - but it's worth a shot ?
    var me = null;

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jtrelfa,

    The change didn't solve the problem, but it makes my code easier to read

    So thanks for the tip!

    What I don't understand is why it sometimes works and sometimes doesn't...

    And I figured out why it always works in Firefox... whenever there's an offending character, it simply converts it to a question mark :S

  4. #4
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only other thing I would suggest is how you're communicating your character set. If you're returning straight XML, you'll do something like this:
    PHP Code:
    <?
    header
    ('Content-type: text/xml');
    echo 
    '<?xml version="1.0" encoding="utf-8" ?>'//or whatever encoding you want
    ?>
    That *should* tell your XML parser what types of characters to expect.

    If you're using the XML to send the data to JavaScript (like an XHR or something), keep in mind that JavaScript assumes it's getting UTF-8 from an XHR.
    var me = null;

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jtrefla,

    It now works with encoding=windows-1256, but not with UTF-8!!

    This should do the trick for the time being, so thank you for the solution!

    I haven't tried it yet, but the XML is meant to be used for an Ajax application, so I don't know how it will be handled by the Javascript.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jtrelfa,

    Sorry, it was my mistake. The file I used to populate the database wasn't UTF-8, which is why it wasn't working...

    Now it even works with UTF-8!

    So, technically I shouldn't have a problem with the Javascript now...

    Thanks for all your help..

  7. #7
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good luck
    var me = null;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •