XML TO ArrayCollection 两种实现方式
<?xml version="1.0" encoding="UTF-8"?>
<projects>
<node Country="暗暗啊" Gold="10" Silver="20" Bronze="30"/>
<node Country="白斑病" Gold="30" Silver="20" Bronze="10"/>
<node Country="常常厂" Gold="20" Silver="40" Bronze="60"/>
<node Country="赌东道" Gold="50" Silver="30" Bronze="10"/>
<node Country="饿饿额" Gold="15" Silver="18" Bronze="46"/>
<node Country="方法法" Gold="61" Silver="52" Bronze="38"/>
</projects>
<mx:HTTPService id="HTTPService1"
url="charts_ColumnChart.xml"
result="initFile(event)" showBusyCursor="true" method="post"
resultFormat="e4x"/>
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
private var ac:ArrayCollection = new ArrayCollection();
function initFile(event:ResultEvent):void{
for each(var p:XML in event.result..node){
var obj:Object=new Object();
obj.Country=p.@Country;
obj.Gold=p.@Gold;
obj.Silver=p.@Silver;
obj.Bronze=p.@Bronze;
ac.addItem(obj);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<projects>
<node>
<Country>暗暗a</Country>
<Gold>10</Gold>
<Silver>20</Silver>
<Bronze>30</Bronze>
</node>
<node>
<Country>暗b啊</Country>
<Gold>10</Gold>
<Silver>20</Silver>
<Bronze>30</Bronze>
</node>
<node>
<Country>暗c啊</Country>
<Gold>10</Gold>
<Silver>20</Silver>
<Bronze>30</Bronze>
</node>
</projects>
<mx:HTTPService id="HTTPService2"
url="charts_ColumnChart1.xml"
result="initFile1(event)" showBusyCursor="true" method="post"
resultFormat="e4x"/>
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
private var ac1:ArrayCollection = new ArrayCollection();
function initFile1(event:ResultEvent):void{
for each(var p:XML in event.result..node){
var xmlobj:XMLList=new XMLList(p);
ac1.addItem(xmlobj);
}
}
XMLList在默认包中,不用import
event.result..node是resultFormat="e4x"的写法,支持event.result(结果)..节点名
例如:
代码如下:
<a>
<b><c><d><e></e></d></c></b>
<b><c><d><e></e></d></c></b>
</a>
event.result..e 直接可以找到两个E节点对象
建议使用方法二进行XML映射,这样基本是自动映射,不用手动对应属性。唯一不足XML代码量增大!
目前本人只测试过这两种xml实现方式,还是没有脱离for循环,没有找到合适FLEX支持转换类,研究ING 有结果与大家分享。