Skip to main content
December 27, 2011
Answered

xml to array is sloooow

  • December 27, 2011
  • 3 replies
  • 924 views

i want to load xml file in array

i use

var ddd:XML;

var ttt:URLLoader =new URLLoader();

ttt.load(new URLRequest("test1.xml"));

ttt.addEventListener(Event.COMPLETE, fdf);

        

var arr2:Array = new Array();

function fdf (e:Event)

{

 

 

 

ddd = new XML(e.target.data);

 

for (var i = 0; i < ddd.drug.length(); i++)

{

 

arr2.push(String(ddd.drug.@naame));

}

       

}

the xml file contain about 2000 record like this

<drug type ="antibiotic" effe ="cephalexin" naame ="ceporex 500tab" form ="tab" dose1 ="500" price ="15" sidee ="allergic to pencillin & -- in marked RF" imgee ="abo_content/drugimge/ceporex 500tab.jpg" barcodeimg ="" dose2 ="50" dose3 ="" temp1 ="" temp2 ="" temp3 ="1"></drug>

when i use this it takes long time  to do this for loop and the swf is shocked for many second until it works

is the xml is so big to be loaded in array in as3

or i have done some thing wrong

and is there any way to solve this

tkanks v.much

This topic has been closed for replies.
Correct answer

finlaly i found the solution

the slow problem come from

loading var from xml  node through for loop

i found that loading all nodes attributes as one var

var ddd:XML;

var ttt:URLLoader =new URLLoader();

ttt.load(new URLRequest("test2.xml"));

ttt.addEventListener(Event.COMPLETE, fdf);

function fdf (e:Event)

{

ddd = new XML(e.target.data);

arr2 = [String(ddd.drug.@naame)]

}

then seperate it different var inside the array

arr2 =  String(arr2[0]).split(",");

it nearly takes no time

but it need to write "," after the naame attribute in xml

3 replies

Correct answer
December 30, 2011

finlaly i found the solution

the slow problem come from

loading var from xml  node through for loop

i found that loading all nodes attributes as one var

var ddd:XML;

var ttt:URLLoader =new URLLoader();

ttt.load(new URLRequest("test2.xml"));

ttt.addEventListener(Event.COMPLETE, fdf);

function fdf (e:Event)

{

ddd = new XML(e.target.data);

arr2 = [String(ddd.drug.@naame)]

}

then seperate it different var inside the array

arr2 =  String(arr2[0]).split(",");

it nearly takes no time

but it need to write "," after the naame attribute in xml

Kenneth Kawamoto
Community Expert
Community Expert
December 27, 2011

Your for loop can be made to run faster by not evaluating length() on each iteration (a bad practise), using uint for the iterator, and looping backwards.

XML is slow though, so if after the loop optimisation it is still not fast enough you'd better look for alternatives. The data can be JSON or better still Flash Object via amfphp (so that there's no need to parse the data.) Or do not parse the XML to an Array  - often no need to do that.

But reading kglad's post - you're not talking about the delay caused by URLLoader, right?

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

kglad
Community Expert
Community Expert
December 27, 2011

(uint is slower than int)

Kenneth Kawamoto
Community Expert
Community Expert
December 27, 2011

Yes it's an interesting topic:

http://blog.stanislavstankov.com/2009/06/as3-optimization-int-vs-uint/

I naturally assumed Number > int > uint in terms of speed but this looks like Adobe AVM fraud - I may file it to them as "bug"

Thanks for the pointer.

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

kglad
Community Expert
Community Expert
December 27, 2011

it takes my computer about 1 second to to that with 2775 drug nodes using your code.