Storing/Saving XML in local asset folder in AIR

和自甴很熟 提交于 2019-12-11 18:50:07

问题


AIR in general seems to be storing the xml file in the path where the app is installed. I am generating an xml and I want to save/store a xml file in local asset folder of AIR application.

Any thoughts on doing this.


回答1:


The File class has some static variables that point to local directories:

From the docs:

  • File.applicationStorageDirectory — a storage directory unique to each installed AIR application
  • File.applicationDirectory — the read-only directory where the application is installed (along with any installed assets)
  • File.desktopDirectory — the user's desktop directory
  • File.documentsDirectory — the user's documents directory
  • File.userDirectory — the user directory

Creating a pointer to the file

Usually you'll want to store files like these in File.applicationStorageDirectory. So to create the file do:

File.applicationStorageDirectory.resolvePath("my-config.xml");

Alternatively, you can let the user choose where to store the file by using File#browseForSave(), which will display a native 'save' window to choose the location.

Writing the content

Open a FileStream for the File in 'write' mode and write an XML string to the file.

var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.writeUTF(myXmlContent);
fs.close();



回答2:


look at http://www.adobe.com/devnet/air/flex/quickstart/articles/xml_prefs.html

http://www.thetechlabs.com/xml/how-to-build-a-contact-manager-in-air-using-xml-part-2/

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html

var prefsFile:File = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("preferences.xml");
stream = new FileStream();
stream.open(prefsFile, FileMode.WRITE);
stream.writeUTFBytes(outputStringXML);

Or something like:

var saveStr:String = xmlToSave.toXMLString();
var file:File = new File('app-storage:/data.xml');
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);    
fs.writeUTFBytes(saveStr);
fs.close();


来源:https://stackoverflow.com/questions/8998058/storing-saving-xml-in-local-asset-folder-in-air

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!