相关链接:http://younglab.blog.51cto.com/416652/287382
XML转ArrayCollection
一般情况直接转换
import mx.collections.ArrayCollection;
import mx.rpc.xml.SimpleXMLEncoder;
import mx.rpc.xml.SimpleXMLDecoder;
private var tip:XML=
<tips>
<tip>
<no>1</no>
<date>2011/10/19</date>
<content>内容</content>
</tip>
<tip>
<no>2</no>
<date>2011/10/31</date>
<content>内容</content>
</tip>
</tips>
private function GetArrayCollection():ArrayCollection{
var xmlDoc:XMLDocument = new XMLDocument(tip);
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder();
var data:Object = decoder.decodeXML( xmlDoc );
var collection:ArrayCollection = new ArrayCollection( data.tips.tip );
return collection;
}
当tip节只有一条数据时,上面转换将报错,需通过Array进行转换
private var tip:XML=
<tips>
<tip>
<no>1</no>
<date>2011/10/19</date>
<content>内容</content>
</tip>
</tips>
private function GetArrayCollection():ArrayCollection{
var xmlDoc:XMLDocument = new XMLDocument(tip);
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder();
var data:Object = decoder.decodeXML( xmlDoc );
var arr:Array = new Array({no:data.tips.tip.no,date:data.tips.tip.date,content:data.tips.tip.content});
var collection:ArrayCollection = new ArrayCollection( arr);
return collection;
}