首先寫一個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][email protected];
_iframeurl_values=xml.ip[1][email protected];
_iframeurl_values_sr=xml.ip[2][email protected];
_iframe_values_qone_zl=xml.ip[3][email protected];
_iframe_values_qone_cb=xml.ip[4][email protected];
_iframe_values_qone_jd=xml.ip[5][email protected];
_iframe_values_qone_zt=xml.ip[6][email protected];
_elapse_timer=xml.ip[7][email protected];
// 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方法中實作變量讀取與顯示。