p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   Classic ASP Basics (http://p2p.wrox.com/classic-asp-basics-61/)
-   -   Using ASP classic filesystem object to retrieve a xml file turns characters into Chin (http://p2p.wrox.com/classic-asp-basics/73150-using-asp-classic-filesystem-object-retrieve-xml-file-turns-characters-into-chin.html)

grotgod March 7th, 2009 01:02 AM

Using ASP classic filesystem object to retrieve a xml file turns characters into Chin
 

Hello;

I have a pretty interesting problem here.

I read an xml file from the server using the fileSystem object. However...the text "magically" turn into Chinese characters.

If I take a look at the xml file in notepad it seems fine (normal English xml), but as soon as I read the individual lines of the file using the filesystem object I get the weird characters.

The code I use to access the file is pretty standard:

var FileObj,this_file;
var txt_arr = new Array();

FileObj = Server.CreateObject("Scripting.FileSystemObject");
this_file = FileObj.OpenTextFile(Server.MapPath("xmlfile.xml") );

while(!this_file.AtEndOfStream)
{
txt_arr[txt_arr.length] = this_file.ReadLine();
}


Response.Write(txt_arr);

Has anybody ever come across something like this before?

philip_cole March 7th, 2009 09:07 AM

Hi

Welcome to P2P.
All I can think is that the file, or possibly your browser, is in some sort of unicode encoding, which may look fine in Notepad but cause problems in the browser.
With the xml file open in Notepad, click File->Save As. At the bottom of the save dialog, there is an Encoding dropdown. Make sure that says Ansi, instead of Unicode or UTF8, and save it.

Note also that Internet Explorer 7 will find the xml tag, think this is an actual xml file being output and attempt to render it as such, whilst Firefox doesn't. This may also be something worth investigating.

Your code is certainly fine, as I dropped it as-is into my own asp file and ran it without any problems.

Hope this helps
Phil

grotgod March 7th, 2009 09:32 AM

RE: Using ASP classic filesystem object to retrieve a xml file turns.....
 
Thanks for the pointers Philip.

Quote:

All I can think is that the file, or possibly your browser, is in some sort of unicode encoding, which may look fine in Notepad but cause problems in the browser.
Good point, but the actual code I use writes the xml file back to the server after making a couple of changes, so in the normal runtime environment it is'nt outputed to the screen.

I checked both files (the file existing on the server and the one created by writing it through the filesystem object) in Dreamweaver and this is how they look.

Basically this:

Code:

<CONFIG>
<TITLE name="Dogs - Abnormal Behaviour - Normal Behaviour"><![CDATA[Introduction Here
]]></TITLE>
<MOVIE url="flashvideo/1.flv" autoplay="true" />
<DATA>
<TEXTS s="0" t="Dogs - problem behaviour - normal behaviour" imgurl="fslides/slide1.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="21.239" t="What are dogs designed to do?" imgurl="fslides/slide2.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="33.13" t="Species responses" imgurl="fslides/slide3.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="62.898" t="Behaviour of the dogs ancestors" imgurl="fslides/slide4.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="142.767" t="Influences on behaviour" imgurl="fslides/slide5.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="173.623" t="Feeling safe" imgurl="fslides/slide6.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="244.832" t="Acquiring food" imgurl="fslides/slide7.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="349.48" t="Acquiring food" imgurl="fslides/slide8.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="431.637" t="Acquiring food" imgurl="fslides/slide9.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="505.864" t="Acquiring food" imgurl="fslides/slide10.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="597.079" t="Reproduction" imgurl="fslides/slide11.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="705.008" t="Social contacts" imgurl="fslides/slide12.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="826.689" t="Further influences on behaviour" imgurl="fslides/slide13.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
<TEXTS s="899.154" t="Summary" imgurl="fslides/slide14.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
</DATA>
</CONFIG>

Becomes this:

Code:

<CONFIG>
਍ऀ㰀吀䤀吀䰀䔀 渀愀洀攀㴀∀䐀漀最猀 ⴀ 䄀戀渀漀爀洀愀氀 䈀攀栀愀瘀椀漀甀爀 ⴀ 一漀爀洀愀氀  䈀攀栀愀瘀椀漀甀爀∀㸀㰀℀嬀䌀䐀䄀吀䄀嬀䤀渀琀爀漀搀甀挀琀椀漀渀 䠀攀爀攀㰀戀爀㸀崀崀㸀㰀⼀吀䤀吀䰀 䔀㸀ഀഀ
<MOVIE url="flashvideo/1.flv" autoplay="true" />
਍ऀ㰀䐀䄀吀䄀㸀ഀഀ
XTS>
਍ऀऀ㰀吀䔀堀吀匀 猀㴀∀㈀㄀⸀㈀㌀㤀∀ 琀㴀∀圀栀愀琀 愀爀攀 搀漀最猀 搀攀猀椀最渀攀搀 琀漀  搀漀㼀∀ 椀洀最甀爀氀㴀∀昀猀氀椀搀攀猀⼀猀氀椀搀攀㈀⸀樀瀀最∀ 㸀㰀℀嬀䌀䐀䄀吀䄀嬀䤀渀琀爀漀搀甀 挀琀椀漀渀 䠀攀爀攀㰀戀爀㸀崀崀㸀㰀⼀吀䔀堀吀匀㸀ഀഀ
<TEXTS s="33.13" t="Species responses" imgurl="fslides/slide3.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
਍ऀऀ㰀吀䔀堀吀匀 猀㴀∀㘀㈀⸀㠀㤀㠀∀ 琀㴀∀䈀攀栀愀瘀椀漀甀爀 漀昀 琀栀攀 搀漀最猀 愀渀挀攀 猀琀漀爀猀∀ 椀洀最甀爀氀㴀∀昀猀氀椀搀攀猀⼀猀氀椀搀攀㐀⸀樀瀀最∀ 㸀㰀℀嬀䌀䐀䄀吀䄀嬀䤀渀琀爀漀 搀甀挀琀椀漀渀 䠀攀爀攀㰀戀爀㸀崀崀㸀㰀⼀吀䔀堀吀匀㸀ഀഀ
<TEXTS s="142.767" t="Influences on behaviour" imgurl="fslides/slide5.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
਍ऀऀ㰀吀䔀堀吀匀 猀㴀∀㄀㜀㌀⸀㘀㈀㌀∀ 琀㴀∀䘀攀攀氀椀渀最 猀愀昀攀∀ 椀洀最甀爀氀㴀∀昀猀氀 椀搀攀猀⼀猀氀椀搀攀㘀⸀樀瀀最∀ 㸀㰀℀嬀䌀䐀䄀吀䄀嬀䤀渀琀爀漀搀甀挀琀椀漀渀 䠀攀爀攀㰀戀爀㸀崀崀 㸀㰀⼀吀䔀堀吀匀㸀ഀഀ
<TEXTS s="244.832" t="Acquiring food" imgurl="fslides/slide7.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
਍ऀऀ㰀吀䔀堀吀匀 猀㴀∀㌀㐀㤀⸀㐀㠀∀ 琀㴀∀䄀挀焀甀椀爀椀渀最 昀漀漀搀∀ 椀洀最甀爀氀㴀∀昀猀 氀椀搀攀猀⼀猀氀椀搀攀㠀⸀樀瀀最∀ 㸀㰀℀嬀䌀䐀䄀吀䄀嬀䤀渀琀爀漀搀甀挀琀椀漀渀 䠀攀爀攀㰀戀爀㸀崀 崀㸀㰀⼀吀䔀堀吀匀㸀ഀഀ
<TEXTS s="431.637" t="Acquiring food" imgurl="fslides/slide9.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
਍ऀऀ㰀吀䔀堀吀匀 猀㴀∀㔀 㔀⸀㠀㘀㐀∀ 琀㴀∀䄀挀焀甀椀爀椀渀最 昀漀漀搀∀ 椀洀最甀爀氀㴀∀昀 猀氀椀搀攀猀⼀猀氀椀搀攀㄀ ⸀樀瀀最∀ 㸀㰀℀嬀䌀䐀䄀吀䄀嬀䤀渀琀爀漀搀甀挀琀椀漀渀 䠀攀爀攀㰀戀爀 㸀崀崀㸀㰀⼀吀䔀堀吀匀㸀ഀഀ
<TEXTS s="597.079" t="Reproduction" imgurl="fslides/slide11.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
਍ऀऀ㰀吀䔀堀吀匀 猀㴀∀㜀 㔀⸀  㠀∀ 琀㴀∀匀漀挀椀愀氀 挀漀渀琀愀挀琀猀∀ 椀洀最甀爀氀㴀∀ 昀猀氀椀搀攀猀⼀猀氀椀搀攀㄀㈀⸀樀瀀最∀ 㸀㰀℀嬀䌀䐀䄀吀䄀嬀䤀渀琀爀漀搀甀挀琀椀漀渀 䠀攀爀攀㰀戀 爀㸀崀崀㸀㰀⼀吀䔀堀吀匀㸀ഀഀ
<TEXTS s="826.689" t="Further influences on behaviour" imgurl="fslides/slide13.jpg" ><![CDATA[Introduction Here
]]></TEXTS>
਍ऀऀ㰀吀䔀堀吀匀 猀㴀∀㠀㤀㤀⸀㄀㔀㐀∀ 琀㴀∀匀甀洀洀愀爀礀∀ 椀洀最甀爀氀㴀∀昀猀氀椀搀攀猀⼀ 猀氀椀搀攀㄀㐀⸀樀瀀最∀ 㸀㰀℀嬀䌀䐀䄀吀䄀嬀䤀渀琀爀漀搀甀挀琀椀漀渀 䠀攀爀攀㰀戀爀㸀崀崀㸀㰀⼀吀 䔀堀吀匀㸀ഀഀ
</DATA>
਍㰀⼀䌀伀一䘀䤀䜀㸀ഀഀ

Amazing is'nt it?

Old Pedant March 7th, 2009 04:46 PM

I tried everything I could to repro what you are seeing without any success at all. My output is normal in all circumstances. So I have to believe that the problem is in your original source file, as Mr. Cole noted.

grotgod March 8th, 2009 12:09 PM

Yes, you could be right Pedant and Mr Cole. I'll take a closer look at the original XML file again.

Thank you very much for your time though.

Old Pedant March 8th, 2009 04:07 PM

He posted this in www.aspmessageboard.com, as well, and uploaded the actual file there.

From my remarks in that forum:

*********

Yep, exactly as predicted. It is a UNICODE FILE!

All you had to do was open it in NOTEPAD and then do SAVE AS... and Notepad would have shown you that it was going to save it as Unicode by default. Meaning that it *is* a Unicode file to start with.

Now go read the docs on Scripting.FileSystemObject.OpenTextFile:
http://msdn.microsoft.com/en-us/libr...4s(VS.85).aspx

See what it says there?
Quote:

format
Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.
So now change your code to do
Code:

this_file = FileObj.OpenTextFile(Server.MapPath("mediaeasy.xml"),1,false,-1 );
Voila! It works.

Note that to write the text back OUT as Unicode you will need to read the docs for CreateTextFile (or use OpenTextFile and overwrite).


All times are GMT -4. The time now is 09:23 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.