0

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;
duTr
  • 714
  • 5
  • 21
adaam
  • 3,700
  • 7
  • 27
  • 51

1 Answers1

1

The FileReference class is safe for web browsers because it doesn't let you mess with users files without his consent.

But, as you are using AIR you can use the File class that extends the FileReference.
Take a look at this link to understand the difference between them.

Here's how you can use the FileStream class:

var file:File = new File().resolvePath("myFile.txt");

var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTF("Hello world!");
stream.close();

And you can read more about the FileStream class here.


EDIT: Looking at your current edited question I'd recommend you to load the xml data with a File/FileReference, something like the following:

private var file:File = new File();

private function loadFile():void 
{
    file.addEventListener(Event.COMPLETE, onFileComplete);
    file.resolvePath("data.xml"); // Or whatever your file is located
    file.load();
}

private function saveFile():void 
{
    var writeStream:FileStream = new FileStream();
    /*
     * FileMode.WRITE will truncate (empty) the file on open.
     * FileMode.APPEND will open the file "as is" and write in the end of it.
     */
    writeStream.open(file, FileMode.WRITE);
    writeStream.writeBytes(data); // Any other write method can be used
    writeStream.close();
}

private function onFileComplete(event:Event):void 
{
    var xml:XML = XML(file.data); // Your XML is here!
    parseXML(xml); // Do your stuff here
}

As you can see, once we get a reference to the file in the file system we can read/write data from/to it freely.

Community
  • 1
  • 1
NemoStein
  • 2,088
  • 2
  • 22
  • 36
  • And how would I adopt this to saving an `XML` object to file like in my example above? (XML object is called `data`) – adaam Apr 22 '13 at 12:51