flex中读取本地外部xml文件方法汇总

    果然好记性不如烂笔头,真的是吃了大亏了。唉!不说了,粘代码。。。。。

 

<?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.rpc.events.ResultEvent;
			
			private var urltreexml:XML;
			[Bindable]
			private var httptreexml:XML;
			[Bindable]
			public var selectedItem:Object;
			
			private function init():void{
				/* HTTPService 方式发送请求 */
				httpsrv.send();
				/* 用 URLLoader方法来获得数据*/
				var urlloadsrv :URLLoader = new URLLoader();
				urlloadsrv.dataFormat=URLLoaderDataFormat.TEXT; 
				urlloadsrv.addEventListener(Event.COMPLETE,handleComplete);
				urlloadsrv.load(new URLRequest("data/treedata.xml")); 
			}
			public function handleComplete(event:Event):void
			{
			    urltreexml=XML(event.target.data); 
			    urlTree.dataProvider = urltreexml;   
			}
			private function httpresultHandler(event:ResultEvent):void {   
				httptreexml = XML(event.result);   
			} 
		]]>
	</mx:Script>
	<!--第一种方式 <mx:Model/>标签 -->
	<mx:Model id="model" source="data/treedata.xml"/>
	<mx:HTTPService id="httpsrv" url="data/treedata.xml" result="httpresultHandler(event)" resultFormat="xml"/>
	<mx:VBox  width="100%" height="100%">
		<mx:HDividedBox width="100%" height="100%"	>
	    	<mx:Panel title="Use the method of 'HTTPService'" width="100%" height="100%">
	    		<mx:Tree id="httpTree" width="100%" height="100%" dataProvider="{httptreexml}" labelField="@label"/>
		    </mx:Panel>
	    	<mx:Panel title="Use the method of 'URLLoader'" width="100%" height="100%">
		    	<mx:Tree id="urlTree"  labelField="@label" width="100%" height="100%" />
		    </mx:Panel>	
	    </mx:HDividedBox>
	   <mx:HBox width="100%">
			<mx:Tree id="source"
				width="250"
				dataProvider="{model.node}"
				change="this.selectedItem=Tree(event.target).selectedItem"/>
			<mx:VBox width="100%" height="100%">
				<mx:Label text="{selectedItem.label}"/>
				<mx:Text text="用Model这种方法,如何让左边的树显示两层,没想出来。" fontSize="12"/>
			</mx:VBox>
		</mx:HBox>
	</mx:VBox>
</mx:Application>

 下面是我的treedata.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<node label="ROOT">
	<node label="Inbox" type="folder">
		<node label="Marketing" type="workspace"/>
		<node label="Product Management" type="workspace"/>
		<node label="Personal" type="workspace"/>
	</node>
	<node label="Outbox" type="folder">
		<node label="Professional" type="workspace"/>
		<node label="Personal" type="workspace"/>
	</node>
	<node label="Spam" type="workspace"/>
	<node label="Sent" type="folder"/>
</node>

 不多说了,大部分都注释了。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值