SoFunction
Updated on 2025-04-14

XML TO ArrayCollection Two implementation methods

one:
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<projects>
<node Country="If there is" Gold="10" Silver="20" Bronze="30"/>
<node Country="White Spot" Gold="30" Silver="20" Bronze="10"/>
<node Country="Changchang Factory" Gold="20" Silver="40" Bronze="60"/>
<node Country="Gambling Host" Gold="50" Silver="30" Bronze="10"/>
<node Country="hungry" Gold="15" Silver="18" Bronze="46"/>
<node Country="Method" Gold="61" Silver="52" Bronze="38"/>
</projects>
<mx:HTTPService
url="charts_ColumnChart.xml"
result="initFile(event)" showBusyCursor="true" method="post"
resultFormat="e4x"/>
import ;
import ;
private var ac:ArrayCollection = new ArrayCollection();
function initFile(event:ResultEvent):void{
for each(var p:XML in ..node){
var obj:Object=new Object();
=p.@Country;
=p.@Gold;
=p.@Silver;
=p.@Bronze;
(obj);
}
}

two:
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<projects>
<node>
<Country>Dark a</Country>
<Gold>10</Gold>
<Silver>20</Silver>
<Bronze>30</Bronze>
</node>
<node>
<Country>Dark b</Country>
<Gold>10</Gold>
<Silver>20</Silver>
<Bronze>30</Bronze>
</node>
<node>
<Country>Dark c</Country>
<Gold>10</Gold>
<Silver>20</Silver>
<Bronze>30</Bronze>
</node>
</projects>
<mx:HTTPService
url="charts_ColumnChart1.xml"
result="initFile1(event)" showBusyCursor="true" method="post"
resultFormat="e4x"/>
import ;
import ;
private var ac1:ArrayCollection = new ArrayCollection();
function initFile1(event:ResultEvent):void{
for each(var p:XML in ..node){
var xmlobj:XMLList=new XMLList(p);
(xmlobj);
}
}

XMLList is in the default package, no import is required
..node is the writing method of resultFormat="e4x", which supports (result)..node name
For example:
Copy the codeThe code is as follows:

<a>
<b><c><d><e></e></d></c></b>
<b><c><d><e></e></d></c></b>
</a>

..e You can directly find two E-node objects
It is recommended to use method 2 for XML mapping, which is basically automatic mapping without manually corresponding attributes. The only shortage of XML code volume increases!
At present, I have only tested these two xml implementation methods, but I still haven't left the for loop, and I haven't found a suitable FLEX support conversion class. The research ING has results to share with you.