views:

55

answers:

3

What way of reading and storing data is fastest for AS3. For debugging right now it is just reading the raw XML, but I suspect it would be faster if I made them into nested arrays.

Would parsing the XML into nested arrays to be read later be the most efficient method?, or is there a better way to read lots of data?

A: 

You could store the data in an Object the first time you parse the XML making it a lot easier/faster to retrieve a set of properties whenever you need them afterwards.

This Object can be viewed as a multidimensional Array if you will.

PatrickS
+1  A: 

Converting into an object may also be advantageous when used in conjunction with the Array.sortOn method. You may also consider using JSON as the transport format; it would remove the overhead of parsing and converting XML.

erkmene
A: 

Well pasring an xml into Array has various advantage as mentioned in above comments.. How you will get Nested Array for the sample 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(new 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;
    }   
Muhammad Irfan