在AS3中存储和解析数据的最有效方法是什么?

时间:2010-09-22 06:05:38

标签: xml actionscript-3 multidimensional-array

AS3以何种方式读取和存储数据的速度最快。对于现在的调试,它只是读取原始XML,但我怀疑如果我将它们变成嵌套数组会更快。

将XML解析为嵌套数组以便以后读取是最有效的方法吗?还是有更好的方法来读取大量数据?

3 个答案:

答案 0 :(得分:1)

当与Array.sortOn方法结合使用时,转换为对象也可能是有利的。您还可以考虑使用JSON作为传输格式;它将消除解析和转换XML的开销。

答案 1 :(得分:1)

将xml转换为Array具有上述评论中提到的各种优势。 如何获得示例XML的嵌套数组

<mynode4    swf="node4.swf" htmlpage="">
<subheading1    swf="L4_s1.swf"     htmlpage="L4_1.htm">Work Hard</subheading1>
<subheading2    swf="L4_s2.swf"     htmlpage="L4_2.htm">karachi</subheading2> 
<subheading3    swf="L4_s3.swf"     htmlpage="L4_3.htm">Newyork City.</subheading3> 
</mynode4>      
<mynode5 swf="five.swf" htmlpage="">Here is the Test for node 5</mynode5>
<mynode6  swf="six.swf" htmlpage="">Last node accessed</mynode6>

var uLoader:URLLoader = new URLLoader() uLoader.addEventListener(Event.COMPLETE,onXMLLoaded) uLoader.load(新的URLRequest(“xmldata.xml”))

public function onXMLLoaded(event:Event):void {

    var loader:URLLoader = URLLoader(event.target);
    var root1:XML = new XML(loader.data);
    var showChilds:XMLList = root1.children();
    var nodes_names2            :Array  =   new Array();
    var nodes_swf2              :Array  =   new Array();
    var nodes_values2           :Array  =   new Array();
    var nodes_pages2            :Array  =   new Array();


    var  take_items:Array   =   recursiveXML(showChilds,nodes_names2,nodes_swf2,nodes_values2,nodes_pages2,0);
    this.nodes_names2       =   take_items[0];
    this.nodes_swf2         =   take_items[1];
    this.nodes_values2      =   take_items[2];
    this.nodes_pages2       =   take_items[3];

    trace( nodes_names2);
    trace( nodes_swf2);
    trace( nodes_values2);
    trace( nodes_pages2);

}

public function recursiveXML(showChilds:XMLList,nodes_names:Array,nodes_swf:Array,nodes_values:Array,nodes_pages:Array,count:int):Array                                                                     {

        for each (var eachChild:XML in showChilds) 
        {
            if(eachChild.hasComplexContent())
            {
                nodes_names[count]          =   new Array((eachChild.children().length()))
                nodes_swf[count]            =   new Array((eachChild.children().length()))
                nodes_values[count]         =   new Array((eachChild.children().length()))
                nodes_pages[count]          =   new Array((eachChild.children().length()))

                nodes_names[count][0]       =   eachChild.name()
                nodes_swf[count][0]         =   eachChild.attribute("swf");
                nodes_values[count][0]      =   "";
                nodes_pages[count][0]       =   "";
                var showChilds:XMLList      =   eachChild.children();
                var take_items:Array        =   recursiveXML(showChilds,nodes_names[count],nodes_swf[count],nodes_values[count],nodes_pages[count] , 1);
                nodes_names[count]          =   take_items[0];
                nodes_swf[count]            =   take_items[1];
                nodes_values[count]         =   take_items[2];
                nodes_pages[count]          =   take_items[3];
                count++;
            }
            else
            {
                nodes_names[count]          =   eachChild.name();   
                nodes_swf[count]            =   eachChild.attribute("swf");
                nodes_pages[count]          =   eachChild.attribute("htmlpage");    
                nodes_values[count]         =   eachChild.text();   
                count++;
            }
        }
            var _items:Array    = new Array( nodes_names, nodes_swf,nodes_values,nodes_pages);
            return _items;
    }   

答案 2 :(得分:0)

您可以在第一次解析XML时将数据存储在Object中,这样可以在以后需要时更轻松/更快地检索一组属性。

如果愿意,可以将此对象视为多维数组。