(续)flex读取本地文件XML

首先写一个AS类.

ReadData.as类如下所示:

package com.utils.allas
{
	
	/**
	 * *
	 * 不写接口,直接写实现类
	 * 
	 * */
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	import mx.utils.StringUtil;
	
	public class ReadData
	{
		private var loader:URLLoader = new URLLoader();
		private var request:URLRequest = new URLRequest("../config/data.xml");
		
		public static var reaDdate: ReadData = null;
		

		
		public var ipUrl:String;
		//这是一个URL,其实是一个XML文件
		
		
		//相关跳转链接
		public var _ip_value :String;
		public var _iframeurl_values:String;
		public var _iframeurl_values_sr:String;
		public  var _iframe_values_qone_zl:String;
		public var _iframe_values_qone_cb:String;
		public var _iframe_values_qone_jd:String;
		public var _iframe_values_qone_zt:String;
		public static var _elapse_timer:String;
		
		
		//URL集合
		public  var UrlArr:Array;
		
		
		public function  get ip_value():String
		{
			return _ip_value;
		}
		

		
		public function get iframeurl_values():String
		{
			return this._iframeurl_values;
		}

		public function get iframeurl_values_sr():String
		{
			return this._iframeurl_values_sr;
		}

		public function get iframe_values_qone_zl():String
		{
			return this._iframe_values_qone_zl;
		}

		
		public function get iframe_values_qone_cb():String
		{
			return this._iframe_values_qone_cb;
		}

		
		public function get iframe_values_qone_jd():String
		{
			return this._iframe_values_qone_jd;
		}

		
		public function get iframe_values_qone_zt():String
		{
			return this._iframe_values_qone_zt;
		}

//		public function get elapse_timer():String
//		{
//			return this._elapse_timer;
//		}
//
//		public function set elapse_timer(_elapse_time:String):void{
//			this._elapse_timer=_elapse_time;
//		}
		public static function getInstence() : ReadData{
			 if (reaDdate == null) {
				 reaDdate = new ReadData();
			 }
			 return reaDdate;
		}
		
		//加载这个构造器
		public function ReadData()
		{
			// TODO Auto-generated method stub
			loader.load(request);
			//	Alert.show(loader.data);
			loader.addEventListener(Event.COMPLETE, onComplete);     
		}
		
		//事件响应函数
		private function onComplete(event:Event):void
		{
			var loader:URLLoader = event.target as URLLoader;
			if (loader != null)
			{
			
				//开始尝试使用Array方式
//				ipUrl = StringUtil.trim(loader.data);
//				//UrlArr = ipUrl.split("\r");
				
				var xml:XML=XML(event.target.data);   

				trace(xml.toString());
				
				
				_ip_value=xml.ip[0].@data;
				_iframeurl_values=xml.ip[1].@data;
				_iframeurl_values_sr=xml.ip[2].@data;
				_iframe_values_qone_zl=xml.ip[3].@data;
				_iframe_values_qone_cb=xml.ip[4].@data;
				_iframe_values_qone_jd=xml.ip[5].@data;
				_iframe_values_qone_zt=xml.ip[6].@data;
				_elapse_timer=xml.ip[7].@data;
			
				
//				trace("共有"+UrlArr.length+"记录");
//				trace("时间:\t"+_ip_value);
	
			}
			else
			{
				Alert.show("不可以加载URLLoader类!");
			}
		}
		
		public function get ipUrlR():String{
		   return ipUrl;
		}
		
		
		
		
	}
}

然后在mxml文件中使用:

getValueFromNativeFile.mxml:

<?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"
			   initialize="application1_initializeHandler(event)"
			   creationComplete="application1_creationCompleteHandler(event)"
			   
			   >
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import com.utils.allas.ReadData;
			
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			
			public var rd:ReadData;
		

			protected function application1_initializeHandler(event:FlexEvent):void
			{
				// TODO Auto-generated method stub
				 
				rd = ReadData.getInstence();
				

			}
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				// TODO Auto-generated method stub
				if (rd != null) {
					Alert.show(rd._iframeurl_values_sr);	 
				}
				
			}
			
		]]>
	</fx:Script>
	
</s:Application>

而用到的xml文件为:

data.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<ips>  
               <ip data="http://192.168.17.22:8080" tp="IP_valuse"/>   
               <ip data="http://192.168.17.101/ppar/" tp="IFrameURL_valuse"  />   
               <ip data="http://www.gzios.com/ppar/bbs/forum.php?mod=viewthread&tid=215&extra=" tp="IFrameURL_valuse_SR"/>   
               <ip data="http://gz.iscas.ac.cn:8080/pm/hl/QualityMonitorAction.do?method=getQualityMonitor_tree&uuid=nZFQlgRWtN6LJCnbVNai1MTXAvwIoJY2" tp="IFrameURL_valuse_Qone_ZL" />   
               <ip data="http://gz.iscas.ac.cn:8080/pm/hl/CostMonitorAction.do?method=getCostMonitor_tree&uuid=pq0q0M8iY3wfIUxSsWoQYL4hF7MjgmZB" tp="IFrameURL_valuse_Qone_CB" />   
               <ip data="http://gz.iscas.ac.cn:8080/pm/hl/ProgressMonitorAction.do?method=getProgressMonitor_tree&uuid=dUmn2pb3vLTjg7Hok6iT5hOH6nTR7365" tp="IFrameURL_valuse_Qone_JD" />   
               <ip data="http://gz.iscas.ac.cn:8080/pm/hl/ProjMonitorAction.do?method=getAllMonitor_tree&uuid=vBEiHa4vjku3vrqFMEeFNlY6ktdjHzYa" tp="IFrameURL_valuse_Qone_ZT" />   
               <ip data="30000" tp="ElapseTimer" />                
</ips>  
 

注意: 在oncomplete函数是要经过两次回调才能返回,所以在mxml文件一个方法中(比如initial方法)中要先new出一个新类,然后在另一个方法createcomplete方法中实现变量读取与显示。


你可能感兴趣的:((续)flex读取本地文件XML)