423,818 Members | 2,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,818 IT Pros & Developers. It's quick & easy.

String encoding Converting and Save File Problem in IE

P: n/a
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.nsIScriptabl eUnicodeConverter);
//uConv = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIUTF8Conve rterService);

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.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


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/de...dobjstream.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.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2

P: n/a
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.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.