使用bindable绑定xml文件

 

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
			   creationComplete="creationComplete()" >
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			/*
			数据的实时更新 ,使用ENTER_FRAME事件
			*/
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.rpc.http.HTTPService;
			import mx.collections.ArrayCollection;
			
			public var service:HTTPService = new HTTPService();
			[Bindable]
			public var xml:XML; 
			public function SetVisits():void
			{
				service.url = "Province.xml";
				service.method = "post";
				service.resultFormat = "e4x"; 
				service.addEventListener(ResultEvent.RESULT, xmlLoaded);
				service.addEventListener(FaultEvent.FAULT,xmlError);
				service.send();
			}
			
			//HTTPService正确时执行的方法
			public function xmlLoaded(e:ResultEvent):void
			{
				xml = service.lastResult as XML;
				
				textarea1.text = xml.toString();
			}
			
			//HTTPService异常时执行的方法
			public function xmlError(e:FaultEvent):void
			{
				trace("出现异常");
			}
			//实时数据更新,ENTER_FRAME
			public function creationComplete():void
			{
				this.addEventListener(Event.ENTER_FRAME,function():void{
					SetVisits();
				});
			}
			
		]]>
	</fx:Script>
	<mx:List width="120" 
			 dataProvider="{xml..name}" x="913" y="51" height="417"  />
	
</s:Application>

 Province.xml文件

 

 

<?xml version="1.0" encoding="UTF-8"?>
<main>
  <province>
    <name>湖南</name>
    <visits>2</visits>
  </province>
  <province>
    <name>湖北</name>
    <visits>43</visits>
  </province>
  <province>
    <name>江西</name>
    <visits>2</visits>
  </province>
  <province>
    <name>浙江</name>
    <visits>43</visits>
  </province>
  <province>
    <name>安徽</name>
    <visits>2</visits>
  </province>
  <province>
    <name>江苏</name>
    <visits>43</visits>
  </province>
</main>
 

 

 

你可能感兴趣的:(xml,Flex,Adobe)