AS3 - 合并XML文件 [英] AS3 - Merging XML Files

查看:149
本文介绍了AS3 - 合并XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个问答游戏是Flash(AS3)与两个不同的类别。玩家可以选择一个类别,或其他的,还是这两类在一起。在code下面我试图合并两个XML文件一起到一个新的XML文件,如果该人选择两个类别。我得到的未定义的属性错误myLoader和myLoader2访问,我不知道为什么。

I'm creating a trivia game is Flash (AS3) with two different categories. The player can choose one category, or the other, or both categories together. In the code below I'm trying to merge the two xml files together into a new xml file if the person selects both categories. I'm getting an "Access of undefined property error for myLoader and myLoader2 and I don't know why.

// start loading of questions
public function xmlImport() 
{
    var myLoader:URLLoader = new URLLoader();
    var myLoader2:URLLoader = new URLLoader();

    if (so.data.question_set == "BOTH")
    {
        myLoader.load(new URLRequest("category1.xml"));
        myLoader2.load(new URLRequest("category2.xml"));
        myLoader.addEventListener(Event.COMPLETE, loadXML2);

        function loadXML2() 
        {
            myLoader2.addEventListener(Event.COMPLETE, combineXML);
        }
    }

    if (so.data.question_set == "ONE")
    {
        myLoader.load(new URLRequest("category1.xml"));
        myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    }

    if (so.data.question_set == "TWO")
    {
        myLoader.load(new URLRequest("category2.xml"));
        myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    }
}

public function combineXML() 
{
    var xmlJoin:XML = <trivia></trivia>;
    var i:XML;

    for each(i in myLoader)
    {
        xmlJoin.appendChild(i);
    }

    for each(i in myLoader2)
    {
        xmlJoin.appendChild(i);
    }

    trace(xmlJoin);
}

感谢您的帮助,您可以提供。

Thanks for any help you can offer.

推荐答案

您可以简单地使用的的appendChild()方法XML 类似以下对象:

You can simply use the appendChild() method of an XML object like the following:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var xml1:XML = <rootNode>
                               <parentNode>
                                   <childNode>1</childNode>
                                </parentNode>
                            </rootNode>;

            var xml2:XML = <rootNode>
                               <parentNode>
                                   <childNode>2</childNode>
                                </parentNode>
                            </rootNode>;

            xml1.parentNode.appendChild(xml2.parentNode.childNode);

            trace(xml1); // output: <rootNode>
                         //             <parentNode>
                         //                 <childNode>1</childNode>
                         //                 <childNode>2</childNode>
                         //             </parentNode>
                         //         </rootNode>

        }// end function

    }// end class

}// end package

[更新]

您也可以简单地合并两个 XML 对象插入的XMLList 对象如下所示:

You can also simply merge both XML objects into an XMLList object like the following:

var xmlList:XMLList = XMLList(xml1.toString().concat(xml2.toString())); // XMLList(xml1 + xml2)

trace(xmlList); // output: <rootNode>
                //             <parentNode>
                //                 <childNode>1</childNode>
                //             </parentNode>
                //         </rootNode>
                //         <rootNode>
                //             <parentNode>
                //                 <childNode>2</childNode>
                //             </parentNode>
                //         </rootNode>

[更新2]

我改写了您的应用程序(以及您的应用程序发布的部分),以显示你的方法,你可能想:

I rewrote your application(well the portion of your application you posted) to show you the approach you may want to take:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.SharedObject;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class Main extends Sprite 
    {
        private var _sharedObject:SharedObject;
        private var _urls:Array;
        private var _xmls:Vector.<XML>;
        private var _xmlsLoaded:int;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            _sharedObject = SharedObject.getLocal("questions");
            _sharedObject.data.category = Category.BOTH;

            _xmls = new Vector.<XML>();

            switch(_sharedObject.data.category)
            {
                case Category.ONE:  loadXml("xml/category1.xml");                      break;
                case Category.TWO:  loadXml("xml/category2.xml");                      break;
                case Category.BOTH: loadXml("xml/category1.xml", "xml/category2.xml"); break;

            }// end switch

        }// end function

        private function loadXml(...urls):void
        {
            _urls = urls;

            for each(var url:String in urls)
            {
                var urlLoader:URLLoader = new URLLoader(new URLRequest(url));
                urlLoader.addEventListener(Event.COMPLETE, onUrlLoaderComplete);

            }// end function

        }// end function

        private function onUrlLoaderComplete(e:Event):void
        {
            _xmls.push(XML(URLLoader(e.target).data));

            if (_urls.length == ++_xmlsLoaded) traceXMLList();

        }// end if

        private function traceXMLList():void
        {
            trace(getXMLList(_xmls)); // output: <category1>
                                      //             <question>question 1</question>
                                      //             <question>question 2</question>
                                      //         </category1>
                                      //         <category2>
                                      //             <question>question 1</question>
                                      //             <question>question 2</question>
                                      //         </category2>


        }// end function

        private function getXMLList(_xmls:Vector.<XML>):XMLList
        {
            var xmlList:XMLList = new XMLList();

            for (var i:uint = 0; i < _xmls.length; i++)
            xmlList += _xmls[i];

            return xmlList;

        }// end function

    }// end class

}// end package

internal class Category
{
    public static const ONE:String = "one";
    public static const TWO:String = "two";
    public static const BOTH:String = "both";

}// end class

这篇关于AS3 - 合并XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆