String encoding Converting and Save File Problem in IE

G

gnv

Hi all,
I am writing a cross-browser(i.e. 6 and netscape 7.1) javascript
program to save an XML file to local file system.

I have an xml string like below:

var xmlStr = "<?xml version="1.0" encoding="UTF-8"?><a>some info</a>";

I want to save this xml file to local file system with JavaScript,

in Netscape 7.1, this is easy with XPCOM:

var uConv = Components.classes['@mozilla.org/intl/scriptableunicodeconverter'].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
//uConv = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIUTF8ConverterService);



uConv.charset = "UTF-8";

var xmlStrUTF8 = uConv.ConvertFromUnicode(fileStr);

then save the utf-8 encoded file to local file system.

But how can I do the same stuff in IE 6.0?

I tried two approaches but not working:
1.
convert the xml string to XML DOM object, try to use the XML DOM save
method, but IE6.0 use msxml 3.0 and it doesn't work by calling save()
from browser due to security issue.

2.
using ActiveX filesystem object's createTextFile method, then call
write() method. But the problem is the write() method only can save a
text file with ASCII encoding or utf-16 encoding. My xml file needs
"utf-8" encoding.

So I am wondering how to do it in IE. or if there is some small free
window utility I can call from Javascript to convert a utf-16 to
utf-8?

Thanks much,

v.
 
M

Martin Honnen

gnv wrote:

I am writing a cross-browser(i.e. 6 and netscape 7.1) javascript
program to save an XML file to local file system.

I have an xml string like below:

var xmlStr = "<?xml version="1.0" encoding="UTF-8"?><a>some info</a>";

I want to save this xml file to local file system with JavaScript,
But how can I do the same stuff in IE 6.0?

I tried two approaches but not working:
1.
convert the xml string to XML DOM object, try to use the XML DOM save
method, but IE6.0 use msxml 3.0 and it doesn't work by calling save()
from browser due to security issue.

As all your stuff is local it seems you could use a HTML application
(.hat file) instead of a .html file, there you should be able to call
save() successfully. Of course .hta is not cross browser.
2.
using ActiveX filesystem object's createTextFile method, then call
write() method. But the problem is the write() method only can save a
text file with ASCII encoding or utf-16 encoding. My xml file needs
"utf-8" encoding.

Any XML parser should be able to cope with both UTF-8 and UTF-16 so I am
not sure why you need UTF-8. I don't see a way to get UTF-8 with the
FileSystemObject unless you know you have only characters in the range
0..127 as there (at least on a Western European Windows) the local
codepage should yield the same encoding as UTF-8.
Some people use ADODB.Stream to read/write binary files with script on
Windows, the docs are at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdobjstream.asp
you might be able to use ADODB.Stream for your purpose. I see you can
even set the Type to text, set a Charset and then use WriteText and
SaveToFile. Thus a quick test with a Windows Script Host JScript

var stream = new ActiveXObject('ADODB.Stream');
stream.Type = 2; // 2 is type text (adTypeText)
stream.Charset = 'UTF-8';
var xml = '<?xml version="1.0" encoding="UTF-8"><root>You have to pay
2.50 € if you use German umlauts ä, ö, ü.<\/root>';
stream.Open();
stream.WriteText(xml);
stream.SaveToFile("test20040602.xml", 2);
stream.Close();

seems to yield an UTF-8 encoded XML file. However it has a byte order
mark it seems at the beginning which some XML parsers don't like.
 
G

gnv

Thank you so much for your help, Martin. I will use ADODB.Stream, then
javascript commandline program to remove the byte order mark as
workaround since I will change my current codebase alot if I use hta.

the reason using UTF-8 instead of UTF-16 is that the majority content
of the XML document is ASCII, so UTF-16 double the file size.

Thanks again!!

v.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top