Flex读取XML文件主要通过以下两种方式:
(1) 通过HTTPService
(2) 通过URLLoader
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
public const xmlUrl:String = "config.xml";
[Bindable] private var colors1:ArrayCollection;
[Bindable] private var colors2:XML;
private function init():void{
//方法一:通过HTTPService
var service:HTTPService = new HTTPService();
service.url = xmlUrl;
service.addEventListener(ResultEvent.RESULT, resultHandler);
service.send();
//方法二:通过URLLoader
var request:URLRequest = new URLRequest(xmlUrl);
var loader:URLLoader = new URLLoader(request);
loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
}
private function resultHandler(event:ResultEvent):void{
colors1 = event.result.colors.color;
}
private function loaderCompleteHandler(event:Event):void{
colors2 = new XML(event.target.data);
}
]]>
</mx:Script>
<mx:List x="100" y="150" dataProvider="{colors1}" labelField="name">
</mx:List>
<mx:List x="300" y="150" dataProvider="{colors2.color}" labelField="@name">
</mx:List>
</mx:Application>
本文介绍使用Flex通过HTTPService和URLLoader两种方式读取XML文件的方法,并提供了具体代码实现。
124

被折叠的 条评论
为什么被折叠?



