xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<image id="1" source="/images/pic1.png" />
<image id="2" source="/images/pic2.png" />
<image id="3" source="/images/pic3.png" />
<image id="4" source="/images/pic4.png" />
<image id="5" source="/images/pic5.png" />
<image id="6" source="/images/pic6.png" />
<image id="7" source="/images/pic7.png" />
<image id="8" source="/images/pic8.png" />
<image id="8" source="/images/pic9.png" />
</root>
两种方法:
1,HttpService方法
<mx:HTTPService id="myService" url="../images.xml" result="resultHandler(event)"/>
[Bindable]
var myData:ArrayCollection=new ArrayCollection();
private function init():void
{
//发送请求
myService.send();
}
private function resultHandler(event:ResultEvent):void
{
myData = event.result.root.image;
}
//应用:
//获得每个image的source显示在list中
<mx:List id="datalist" dataProvider="{myData}" labelField="@source"/>
//获得某个image的source
<mx:Text id="txt" text="{myData.getItemAt(0).source}"/>
2,URLLoader方法
[Bindable]
var myXML:XML;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("../images.xml");
private function init():void
{
loader.load(request);
loader.addEventListener(Event.COMPLETE, onComplete);
}
private function onComplete(event:Event):void
{
var loader:URLLoader = event.target as URLLoader;
myXML=new XML(loader.data);
}
//应用:
//获得每个image的source显示在list中
<mx:List id="datalist" dataProvider="{myXML.image}" labelField="@source"/>
//获得某个image的source
<mx:Text id="txt" text="{myXML.image[0].@source}"/>
Flex4中,不用加@
更详细的关于flex操作xml的文章地址:http://www.cnblogs.com/chengying/archive/2011/08/11/chengying.html