[Project_owners] XMLHttpRequest characterset weirdness

Matthew Wilson matthew at mjwilson.demon.co.uk
Tue Oct 5 23:10:47 EDT 2004


Sudhakar Chandra wrote:
> Matthew Wilson wrote:
> 
>> (code snipped)
>>
>> It sounds to me as though the content is sending a character encoding 
>> as  an HTTP header which is honoured by XMLHttpRequest when it 
>> constructs the DOM object, but lost when you save the raw text to file.
>>
>> You may need to re-serialize the responseXML object with a specified 
>> character encoding (eg UTF-8).
> 
> Thanks for that quick answer.  Could you point me towards a code snippet?

Something like this:

     var serializer = new XMLSerializer();

     // This is a BIG HACK but works OK at the moment
     var str = Components.classes["@mozilla.org/supports-string;1"]. 

           createInstance(Components.interfaces.nsISupportsString);
     str.data = '';
     var outputStream = {
         write: function(buf, count) {
            str.data += buf.substring(0,count);
            return count;
        }
     };
     serializer.serializeToStream (xmlhttp.responseXML, outputStream, 
"UTF-8");
     var targetDocument = "<?xml version='1.0' encoding='UTF-8'?>" + 
str.data;


The BIG HACK is to work around the lack of a scriptable output stream in 
Mozilla (as far as I am aware).

Matthew



More information about the Project_owners mailing list