源文出处: http://www.flashdev.ca/article/loading-and-saving-xml-data-in-air-apollo/
看到此文, 挺有用, 把它翻译过来, 英语水平有限, 有哪里看不明白的就向我仍砖吧..哈哈.... 也恳请高手们指点指点....先放谢了.... :
...... 我意识到, 在Flash里加载xml比在AIR里加载要来得方便, 但在AIR加载xml有一个优势就是: 我可以使用包含在AIR package里的 File.applicationResourceDirectory 来瞄准xml的安装目录.
在下面的代码片段里, 我使用同步打开的方法, 其本质就是: 暂停所有在Flash影片里运行的东西直到代码完成它的任务(打开文件):
-------------------------------------------------------
import flash.filesystem.*;
private var _data:String; // Data string pulled from the xml file.
/**
* Load the xml file and read its data.
*/
private function loadData():void{
var dataFile:File = File.applicationResourceDirectory.resolve("data.xml");
var stream:FileStream = new FileStream();
stream.open(dataFile, FileMode.READ);
_data = stream.readUTFBytes(stream.bytesAvailable);
stream.close();
}
-------------------------------------------------------
在下面的代码将会显示怎样通过使用异步方法保存xml数据, 异步方法允许Flash影片正常地运行且可以通过触发事件来达到用户指定的动作:
-------------------------------------------------------
private var _dataXML:XML; // The XML data.
/**
* Save the modified data to the xml file.
*/
private function saveData():void{
var dataFile:File = File.applicationResourceDirectory.resolve("data.xml");
var stream:FileStream = new FileStream();
stream.openAsync(dataFile, FileMode.WRITE);
stream.writeUTFBytes(_dataXML);
stream.addEventListener(Event.CLOSE, onDataSaved);
stream.close();
}
/**
* Called when the data has been successfully saved.
* Load the saved data back into the application.
*/
private function onDataSaved(event:Event):void{
loadData();
}
-------------------------------------------------------
如果你计划使用AIR来开发你的项目, 我强烈推荐你看看Apollo for Adobe Flex Developers Pocket Guide ( by Mike Chambers, Robert L. Dixon & Jeff Swartz )
回复Comments
{commenttime}{commentauthor}
{CommentUrl}
{commentcontent}