Search code examples
xmlactionscript-3flashair

Saving an XML file with adobe air creates bogus symbols at the start


I'm making an app that saves and loads attributes in an XML file. Everything is working as it should, except for one thing.

function save(){
    var item:XML = new XML(<node></node>);

    var file = new File();
    file = File.applicationStorageDirectory.resolvePath("generated.xml");

    //children of the main node and attributes and stuff are managed here but
    //they're working fine so i've removed them from this example.

    var fileStream = new FileStream();
    fileStream.open(file, FileMode.WRITE);
    fileStream.writeObject(item);
    fileStream.close();
}

When the above code gets executed, it should save an xml that looks like this:

<node/>

or if it has a child within it:

<node>
    <child> whatever </child>
</node>

However, when i open the XML file in sublime text 2, it looks like this

http://i46.tinypic.com/15h1k5h.png

and the CS6 debugger throws Error #1088: The markup in the document following the root element must be well-formed.

If i manually go in and remove the bogus characters, air is able to read it just fine, but when i save any changes to the XML, i'll have to manually go in and remove the bogus characters again.

Do you have any ideas on how i could fix this? Thank you.

PS: The bogus characters seem to change. [VT] has been the same every time, but [SI] does not stay the same. I don't know what this means or if it's relevant, but it might be.


Solution

  • You don't want to use writeObject here. Except on Strings and, maybe, Numbers, writeObject actually writes data that is relative to the actual object (public properties, memory identifier, etc). You want to either save the String version of the XML (XML.toString() should work) or use writeUTFBytes(). I have personally never used writeUTFBytes(), but if my understanding is correct, it will only write the text of the object and not the object data itself. I would give that a try as well.