Flex 学习笔记 XML 转ArrayCollection

相关链接:http://younglab.blog.51cto.com/416652/287382

XMLArrayCollection

一般情况直接转换

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;

}

posted @ 2011-11-08 11:50  Anlycp  阅读(666)  评论(0编辑  收藏  举报