JOSEFtw
JOSEFtw

Reputation: 10081

XML 5617: Illegal XML character Internet Explorer 9

I have the following xml file:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
  <branch>
     <name>Ale Torg</name>
     <address>Ale Torg</address>
     <city>NÖDINGE</city>
     <phone>0303-    336730</phone>
     <clnr>6854</clnr>
     <open>må-on 10-16, to 10-18, fr 10-16</open></branch>
 <branch>
     <name>Alfta</name>
     <address>LÅNGGATAN 59</address>
     <city>ALFTA</city>
     <phone>0271-61960</phone>
     <clnr>6402</clnr>
     <open>må 9-17, ti-on 9-15, to 9-17, fr 9-15</open>
</branch>
<branch>
    <name>Alingsås</name>
    <address>Kungsgatan 24</address>
    <city>ALINGSÅS</city>   
    <phone>0322-667280</phone>
    <clnr>6832</clnr>
    <open>må 10-18, ti-on 10-15, to 10-18, fr 10-15</open>
</branch>
<branch>
      <name>Alvesta</name>
      <address>Centralplan</address>
      <city>ALVESTA</city>
      <phone>0472-267760</phone>
      <clnr>6679</clnr>
      <open>må-on 10-15, to 10-18, fr 10-15</open>
</branch>
</root>

And the following js to parse it:

function encodeSwedishChars(data)
{
      data = data.replace(/å/g, '%E5');
      data = data.replace(/ä/g, '%E4');
      data = data.replace(/ö/g, '%F6');
      data = data.replace(/Å/g, '%C5');
      data = data.replace(/Ä/g, '%C4');
      data = data.replace(/Ö/g, '%D6');
      data = data.replace(/é/g,'%E9');
      data = data.replace(/É/g, '%C9');

      return data;
}

function addAutoComplete()
{
  //Use datatype TEXT so jquery doesnt parse it.
  var officeNames = [];
  $.ajax({
    url: "offices2.xml",
    dataType: "text",
    type: "GET",
    success: function(xml)
    {
      xml = xml.encodeSwedishChars();
      xml = $.parseXML(xml);

      var names = $(xml).find("branch").each(function(){
        var text = $(this).find('name').text();
        officeNames.push(text);

        var office = {};
        office.name = text;
        office.clnr = $(this).find("clnr").text();

        namesAndClnr.push(office);

      });
    }
  });

  $( "#guide-lastpage-form-p9" ).autocomplete({
      source: officeNames,
    })
}

The code works fine in all browsers I tested, but not in Internet Explorer 9... I get the following error:

SCRIPT5022 Invalid XML: <?xml version=1.0" encoding="ISO-8859-1" ?>
jquery.min.js, line 4 character 4106

XML5617: Illegal XML character
, line 3 characther 66 (This is the letter Ö the first branch, city, Nödinge)

I cant change anything in the XML-file because i dont have access to it, offices2.xml is only a testfile with the exact same data as I recieve on the live site.

Why doesn't this work? I think that my escapeSwedishChars method would fix it?

I HAVE NO ACCESS TO THE XML FILE SO I CANT CHANGE THE ENCODING TO UTF-8

Upvotes: 1

Views: 2742

Answers (1)

opalenzuela
opalenzuela

Reputation: 3171

(Disclaimer: although is not a complete answer to the question, it was too long to put it as a comment, and I believe this information provided can save time to others, so I place it as an answer. Have mercy with downvotes!)

Microsoft's solution for this HUGE BUG it's this:

Specify the proper encoding scheme in the XML processing instruction.

  • or -

Re-encode the XML data as proper UTF-8.

Source: http://support.microsoft.com/kb/238833

Unfortunately, the second let's say "solution" is not applicable in most of the cases, and the first option is useless in most of the environments where we are only requesting the information via Ajax.

In my case, I've found two different bad behaviours, depending on the objects I use to request the data. When I use XMLHttpRequest, the resulting responseText is already corrupted (of course responseXML is undefined) and there is very little to do with it.

However, I've managed to make the request by using the XDomainRequest object (which is not supposed to be used for this purpose) but at least the responseText is still readable. Unfortunately, the char values of all the non-ASCII characters are set to 65533, which is the code for non-printable characters, hence the best I can achieve is to read them and replace for some particular marker which makes a ö indistinguishable from a Ä. But at least the resulting XML is well-formed and I can read the data.

Upvotes: 1

Related Questions