Search code examples
androidiosactionscript-3flashair

How Can I Save My XML File To A Specific Folder?


I have an app (Android & IOS) that contains an XML file (embedded with Adobe Flash in settings).

Any idea how to do these manipulations with AS3 code?

  1. Find the exact URL of the xml file.

  2. Move the xml file in a specific folder.

Here's what I've tried :

1) I've tried to load the file via URLLoader and then save it in a specific folder (without success).

var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("soleil.xml"));
myLoader.addEventListener(Event.COMPLETE, complete_handler);

function complete_handler (event:Event) :void
{
    var myXML:XML;
    myXML = new XML(event.target.data);
    var fr:FileReference = new FileReference();
    fr.save(myXML, 'soleil.xml');

    var fileStream:FileStream = new FileStream();
    trace(File.applicationDirectory.nativePath);
    fileStream.open(new File(File.applicationDirectory.nativePath+"\\soleil.xml"),FileMode.WRITE);
}

The last line throws an error Error #2004: One of the parameters is invalid:

fileStream.open(newFile(File.applicationDirectory.nativePath+"\\soleil.xml"),FileMode.WRITE);

2) Just to know where is saved my embed xml file soleil.xml. But I think, it only creates a path...

var file:File = File.applicationStorageDirectory; 
file = file.resolvePath("soleil.xml");
trace(file.url);

EDIT 2 :

The XML file contains data that I want to use.

What I was doing before using File was :

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("soleil.xml"));
myLoader.addEventListener(Event.COMPLETE, processSoleil);

        function processSoleil(e:Event):void {
            myXML = new XML(e.target.data);
    var results:XMLList = myXML.row.(date == mySavedData.data.theDate);
    if(results.length()){

    for each(var row:XML in results){
        //doingstuff

    }
}
        }

Should I change my method ?

Now that I've managed to copy soleil.xml in a specific path by doing :

var packagedFile:File = File.applicationDirectory.resolvePath( "soleil.xml" );
var destFile_soleilXML:File = File.documentsDirectory.resolvePath( "soleil.xml" );
packagedFile.copyTo( destFile_soleilXML );

I though I could load the file by doing :

myLoader.load(new URLRequest(destFile_soleilXML.nativePath));

Shouldn't I do that ?

At the moment I've got an error when trying to load my xml file (Stream error (apparently meaning that "file is not found" ) ).

Any tips ?


Solution

  • On Android your application files are packaged inside the APK and accessed directly from there. As these files are generally compressed there is no direct path (nativePath) to these files. You will need to use the File class to read / extract / copy the data.

    Files packaged with your application are stored relative to the File.applicationDirectory:

    var packagedFile:File = File.applicationDirectory.resolvePath( "soleil.xml" );
    

    To copy this to a location where you can get a native path. The best and most consistent place is the File.applicationStorageDirectory:

    var destFile:File = File.applicationStorageDirectory.resolvePath( "soleil.xml" );
    
    packagedFile.copyTo( destFile );
    

    You should now be able to access the native path of the copied file:

    trace( destFile.nativePath );