0

i need to sort a large XML file and i've been reading that placing each of the elements into an array is the way to sort the data using Array's sortOn() method.

i'm concerned about speed. perhaps storing them into a Vector.< XML > instead of an Array will offer a slight increase in speed, or not, but is this approach the best way to sort XML data?

Chunky Chunk
  • 16,553
  • 15
  • 84
  • 162

3 Answers3

2

Actually, I see two solutions :

1/ You load a static xml file and therefore you can do sorting manually.

2/ You load a dynamic xml and to gain speed, server side can provide sorting (easily in PHP). i think this is the better thing to do.

So in the two case, you have to parse your xml once, store data (Vector, Value Object) and clear the memory by destroying the reference to your xml.

If you want to sort on Flash side, you have to create your own function and using the method sortOn seems inevitable.

kero_zen
  • 704
  • 1
  • 9
  • 22
  • PHP sorting on the server is probably the best idea, but in my case i don't own or maintain the XML data, so perhaps just sorting it in ActionScript would be both easier to maintain and faster (or just as fast) than filtering it on my own webserve before sending it to Flash. – Chunky Chunk Nov 10 '10 at 17:37
  • 1
    And for killing XML, you can use : http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/system/System.html#disposeXML() : ) – OXMO456 Nov 11 '10 at 00:47
  • i guess my last comment didn't make much sense since i forgot to mention that i'm building an AIR application, not a browser resident .swf. so i guess for an AIR application my solution would be ideal, but for a website with Flash, your solution would be best. – Chunky Chunk Nov 11 '10 at 06:00
1

@TheDarklnl1978 Here is part of the source code from fl.data.DataProvider that transform an XML in an Array (found in Flash CS5 install directory):

var xml:XML = obj as XML;
retArr = [];
var nodes:XMLList = xml.*;
for each (var node:XML in nodes) {
    var obj:Object = {};
    var attrs:XMLList = node.attributes();
    for each (var attr:XML in attrs) {
        obj[attr.localName()] = attr.toString();
    }
    var propNodes:XMLList = node.*;
for each (var propNode:XML in propNodes) {
        if (propNode.hasSimpleContent()) {
            obj[propNode.localName()] = propNode.toString();
        }
    }
    retArr.push(obj);
}
return retArr;
OXMO456
  • 3,558
  • 2
  • 25
  • 35
0

so, to answer my own question, i believe that creating a DataProvider object with the XML is the best solution.

assigning the XML to the DataProvider doesn't require setting up and iterating thru a loop, although there may be an internal loop happening behind the scenes. i would be interested to know if it does or not, so please leave a comment if you know.

additionally, DataProvider objects also support sorting, retrieval and flexibility for the creation of lists, data grids, etc.

Chunky Chunk
  • 16,553
  • 15
  • 84
  • 162
  • i believe this solution is ideal for an AIR application, while kero_zen's solution is best for browser-resident .swf – Chunky Chunk Nov 11 '10 at 06:07