AS3 - 获取,创建和保存XML文件

时间:2012-08-15 16:02:37

标签: xml actionscript-3 file setter getter

我正在制作一款名为QuizMaker的游戏 游戏的测验是外部.xml文件,QuizMaker将通过我的类QuizXML导入。 每个测验都包含有答案的问题,可能是图像的形式,也可能是文本或其他形式。 玩家应该能够在QuizMaker中导航“我的测验”文件夹 为此,我需要该文件夹中的文件列表 这是未完成的getter函数,我的问题是:如何获取文件列表?

//returns an array of paths to folders and/or .xml files from the folder "My Quizzes"  
public function getFolders():Array {  
    var folders:Array = new Array();  
    //if .xml or a folder is the file looking at, add the file path to folders  
    return folders;  
}  

//returns an array of .xml file paths from an array consisting of file paths  
//if a folder, which contains .xml files, is encountered from the array, add the .xml file paths to an array  
public function getQuizzes(folders:Array):Array {  
    var validXML:Array = new Array();  
    for(var curPath:uint=0;folders.length>curPath;curPath++) {
        //if folders[curPath] is a folder  
            //somehow look through the folder's content
            //if .xml is the file looking at, add the file path to validXML
        //else if .xml is the file looking at, add the file path to validXML  
    }
    return validXML;  
}  

我的第二个问题是,如何将XML文件保存在“我的测验”文件夹中? 这些是未完成的setter函数:

//creates new folders inside "My Quizzes" from an array, which contains the folders' names as strings
//if a folder with the same name is encountered, don't override that folder
public function setFolders(folderNames:Array):void {  
    for(var curPath:uint=0;folderNames.length>curPath;curPath++) {  
        //if a folder exists as folderNames[curPath]  
            //do nothing  
        //else create a folder called folderNames[curPath]  
    }  
}  

//creates new .xml files from an array, which contains XML objects, inside a folder from a string  
//if an .xml file with the same name as an XML object's nameFile is encountered, don't override that .xml file  
public function setQuizzes(outputPath:String,objectsXML:Array):void {  
    for(var curXML:uint=0;objectsXML.length>curXML;curXML++) {  
        //if an .xml file exists as objectsXML[curXML].nameFile  
            //do nothing  
        //else create objectsXML[curXML] as an .xml file  
    }  
}  

感谢您阅读我的问题。

1 个答案:

答案 0 :(得分:0)

FileReference可能正是您要找的。

它允许用户浏览文件和应用程序以保存到文件系统。

private function save():void
{
    var bytes:ByteArray = new ByteArray();
    var fileRef:FileReference = new FileReference();
    bytes.writeMultiByte("<root>", "iso-8859-1");
    //write your xml here
    bytes.writeMultiByte("</root>", "iso-8859-1");

    fileRef.save(bytes,"savedata.xml");
}