0

我当前用于制作 XML、添加数据并保存它的代码是:

import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.events.Event;

stop();

btn_exit.addEventListener(MouseEvent.CLICK, exitConfirm);  //IGNORE THIS
btn_submit.addEventListener(MouseEvent.CLICK, submitData);   //Submit data event

//IGNORE THIS
function exitConfirm(e:MouseEvent):void {
    gotoAndPlay(5);
}


//On "CLICK", Submit data event
function submitData(e:MouseEvent):void {
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, loadXML);
    loader.load(new URLRequest("events.xml"));

    function loadXML(e:Event):void {
        xml = new XML(e.target.data);
        trace(xml);
    }

    var file:FileReference = new FileReference;
    var app:XML = <app/>; 

    app.titles = comp_title.text;
    app.titles.category = comp_category.text;
    app.titles.place = comp_place.text;
    app.titles.dateDay = null;
    app.titles.dateMonth = null;
    app.titles.dateYear = null;
    app.titles.gear = null;

    file.save(app, "events.xml");
}

我的问题是它只在 XML 文件中创建此代码。我已经尝试在其中添加更多代码以首先在现有 XML 上查找数据,然后获取该数据并添加新数据,但它似乎不起作用。我什至不确定它是否可能,所以这就是我来这里的原因。任何帮助都是好帮助!

PS AS3 对我来说还是相当新的。我对此并不可怕,但我也不好:)

干杯! :D

4

1 回答 1

1

在将更新的 XML 整体保存到文件系统之前,您需要加载现有的 XML 并添加新数据:

  1. 在 ActionScript 3.0 中加载 XML
  2. 在 ActionScript 3.0 中使用 XML

编辑:

以下应该工作。我已经包含评论来解释某些决定:

import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.FileReference;
import flash.events.MouseEvent;

btn_exit.addEventListener(MouseEvent.CLICK, exitConfirm);  //IGNORE THIS
btn_submit.addEventListener(MouseEvent.CLICK, submitData);   //Submit data event

// store path in a variable so we don't need to write it out each time we need it
var xmlPath:String = "events.xml";
var xml:XML;

var loader:URLLoader;

//IGNORE THIS
function exitConfirm(e:MouseEvent):void {
   gotoAndPlay(5);
}

loadXML();

// Best bet is to load the existing XML up-front because you can only trigger the 
// FileReference save method from a handler invoked directly from a user interaction. 
// See: http://stackoverflow.com/questions/3301839/flexs-filereference-save-can-only-be-called-in-a-user-event-handler-how-ca
function loadXML():void {
    loader = new URLLoader();
    loader.addEventListener(IOErrorEvent.IO_ERROR, xmlLoadErrorHandler);
    loader.addEventListener(Event.COMPLETE, xmlLoadHandler);
    loader.load(new URLRequest(xmlPath));
}

function xmlLoadHandler(e:Event):void {
    xml = new XML(e.target.data);
    removeListeners();
    trace("Loaded XML", xml);
}

// Handle the possibility of path not existing
function xmlLoadErrorHandler(e:IOErrorEvent):void {
    xml = <app></app>;
    removeListeners();
    trace("Error, XML did not exist, creating new empty XML");
}

// Process the data and save to file system
function submitData(e:MouseEvent):void {
    var file:FileReference = new FileReference();

    // Append the new title node to the existing XML. 
    // From now on, XML in memory matches exactly what is on file-system
    xml.appendChild(getTitle()); 

    file.save(xml, xmlPath);
}

// Returns a populated title node
function getTitle():XML {
    var xml:XML = <title></title>

    //replace hardcoded strings with your variables
    xml.category = "category"; 
    xml.place = "place";
    xml.dateDay = "day";
    xml.dateMonth = "month";
    xml.dateYear = "year";
    xml.gear = "gear";

    return xml;
}

// It's good practice to remove event listeners when you're finished with them
function removeListeners() {
    loader.removeEventListener(Event.COMPLETE, xmlLoadHandler);
    loader.removeEventListener(IOErrorEvent.IO_ERROR, xmlLoadErrorHandler);
}
于 2012-06-22T02:01:32.327 回答