FileStream injecting strange characters
captain_code-KOo9rr Dec 15, 2008 1:03 PMHello I am having a very strange issue. I have an application
that is driven by XML files, and so I am in the processes of
creating an AIR application to make the process of creating those
XML files very easy. However, I am running into some very strange
issues when I write out with a FileStream object. I have an XML
object that contains the data, from which I use the
XML.toXMLString() method to get an appropriately created XML
string. However, when I write that out to a file on the HDD, I am
getting strange characters at the beginning of the file that are
causing the XML's to be un-usable. If I go in with a binary file
editor and remove these, the XML will parse completely. Below is my
sample code of a
very basic example that causes this issue.
[code]
import flash.filesystem.*;
import flash.events.*;
import flash.net.ObjectEncoding;
var xml:XML = new XML("<?xml version=\"1.0\" encoding=\"utf-16\"?> <Lesson xmlns:virgo=\" http://namespaces.visuallinklanguages.com/virgo/\"> <virgo:Title> </virgo:Title> <virgo:Dialogue image=\"\"> <virgo:Line> <virgo:Speaker>Jimmy:</virgo:Speaker> <virgo:Segment> <virgo:Audio>holaSenor.mp3</virgo:Audio> <virgo:TranslationText>Hola Señor!</virgo:TranslationText> <virgo:EnglishText>Hello Sir!</virgo:EnglishText> </virgo:Segment> </virgo:Line> <virgo:Line> <virgo:Speaker>Paco:</virgo:Speaker> <virgo:Audio>queTal.mp3</virgo:Audio> <virgo:TranslationText>Que tal?</virgo:TranslationText> <virgo:EnglishText>How are you?</virgo:EnglishText> </virgo:Line> </virgo:Dialogue> </Lesson>");
var data:String = xml.toXMLString();
var stream:FileStream = new FileStream();
var file:File = new File();
file.addEventListener(Event.SELECT, saveData);
file.browseForSave("File to write");
function saveData(evt:Event):void
{
stream.objectEncoding = ObjectEncoding.AMF0;
stream.endian = "littleEndian";
stream.open(file, FileMode.WRITE);
stream.writeObject(data);
//stream.writeMultiByte(data, "unicode");
stream.close();
}
[/code]
You'll notice that I have tried multiple ways to write the data. Each results in different ascii characters being placed at the beginning. the write object method typically will insert either two STX (start of text [0x02]) characters, or an STX and an EOT (End of transmission [0x04]). Has anyone else had this issue? I have spent countless hours trying to find a way to make this work, but it won't and my deadline for this has come and gone.
Thanks for any input.
[code]
import flash.filesystem.*;
import flash.events.*;
import flash.net.ObjectEncoding;
var xml:XML = new XML("<?xml version=\"1.0\" encoding=\"utf-16\"?> <Lesson xmlns:virgo=\" http://namespaces.visuallinklanguages.com/virgo/\"> <virgo:Title> </virgo:Title> <virgo:Dialogue image=\"\"> <virgo:Line> <virgo:Speaker>Jimmy:</virgo:Speaker> <virgo:Segment> <virgo:Audio>holaSenor.mp3</virgo:Audio> <virgo:TranslationText>Hola Señor!</virgo:TranslationText> <virgo:EnglishText>Hello Sir!</virgo:EnglishText> </virgo:Segment> </virgo:Line> <virgo:Line> <virgo:Speaker>Paco:</virgo:Speaker> <virgo:Audio>queTal.mp3</virgo:Audio> <virgo:TranslationText>Que tal?</virgo:TranslationText> <virgo:EnglishText>How are you?</virgo:EnglishText> </virgo:Line> </virgo:Dialogue> </Lesson>");
var data:String = xml.toXMLString();
var stream:FileStream = new FileStream();
var file:File = new File();
file.addEventListener(Event.SELECT, saveData);
file.browseForSave("File to write");
function saveData(evt:Event):void
{
stream.objectEncoding = ObjectEncoding.AMF0;
stream.endian = "littleEndian";
stream.open(file, FileMode.WRITE);
stream.writeObject(data);
//stream.writeMultiByte(data, "unicode");
stream.close();
}
[/code]
You'll notice that I have tried multiple ways to write the data. Each results in different ascii characters being placed at the beginning. the write object method typically will insert either two STX (start of text [0x02]) characters, or an STX and an EOT (End of transmission [0x04]). Has anyone else had this issue? I have spent countless hours trying to find a way to make this work, but it won't and my deadline for this has come and gone.
Thanks for any input.



