I am trying to save data gathered from within my app to it's accompanying external xml file called data.xml
. I am using the fileReference.save
method but it just doesn't seem to be doing the job:
function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
var itemIndex:uint = event.currentTarget.index;
delete data.item[itemIndex];
fileReference.save(data, "data.xml");
trace(xmlData);
}
Unfortunately, this causes a 'Save As' dialog box to pop up asking the user where they would like to save the xml file (when what I really want to happen is for the data.xml file in the project's directory to be overwritten automatically without any say on the part of the user).
I have heard that FileStream
is the way to go (especially being I am going to deploy this on Air for iOS), but I don't know how to implement it.
Any ideas?
EDIT: I have updated my question with the suggestion below, but it still doesn't work. The data.xml
file is in the same directory called 'RecipeMatcher2' on my Desktop i.e. /Desktop/RecipeMatcher2/ (I am using Mac OS X), but I also want it to resolve to whatever path the file will be stored in when on the iPhone.
function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
var itemIndex:uint = event.currentTarget.index;
file = File.applicationStorageDirectory;
file = file.resolvePath("data.xml");
delete data.item[itemIndex];
var writeStream:FileStream = new FileStream();
writeStream.open(file, FileMode.WRITE);
writeStream.writeUTFBytes(data.toXMLString());
writeStream.close();
trace('xml file saved');
}
I originally load my xml data like so:
loader = new URLLoader();
// listens to see if data has fully loaded//
loader.addEventListener(Event.COMPLETE, dataLoaded);
//loads local xml file with URLrequest method//
loader.load(new URLRequest("data.xml"));
and here are my public vars relevant to this question:
public var loader:URLLoader;
public var file:File;
public var data:XML;
public var items:XMLList;