XML数据问题

时间:2012-06-22 00:00:21

标签: xml actionscript-3 actionscript

我目前制作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上的数据然后获取并添加新数据,但它似乎不起作用。我甚至不确定它是否可能,所以这就是我来这里的原因。任何帮助都是很好的帮助!

P.S。 AS3对我来说还是一个新手。我不是很可怕,但我不是很好:)。

干杯! :d

1 个答案:

答案 0 :(得分:1)

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

  1. Loading XML in ActionScript 3.0
  2. Working with XML in ActionScript 3.0
  3. 编辑:

    以下内容应该有效。我已经包含了解释某些决定的评论:

    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);
    }