【问题标题】:XML Data IssuesXML 数据问题
【发布时间】:2012-06-22 00:00:20
【问题描述】:

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

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

干杯! :D

【问题讨论】:

    标签: xml actionscript-3 actionscript


    【解决方案1】:

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

    1. Loading XML in ActionScript 3.0
    2. Working with XML in ActionScript 3.0

    编辑:

    以下应该有效。我已经包括 cmets 来解释某些决定:

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

    【讨论】:

    • 好的,我试过了,但它只是表明“xml = new XML(e.target.data)”的“xml”是一个未定义的属性?对此有任何帮助,我认为我是免费的,因为这是错误框中唯一出现的内容:D
    • 您能否更新您的问题以包含您用于加载 XML 的代码?
    • 好的,我已经找出问题所在并已解决,但它仍然没有将新数据添加到旧数据中,而是再次覆盖它:/
    • 天啊!!你是绝对的冠军!我已经坚持了 3 天,试图加载该文件!我不能感谢你!谢谢你,谢谢你,谢谢你!! :D
    • 很高兴我能够帮助 Jess。
    猜你喜欢
    • 2011-10-26
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多