异步加载新浪博客的RSS,节点展开时开始读取。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import flash.utils.Dictionary; import mx.events.TreeEvent; import mx.rpc.AsyncToken; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; private var dictionary:Dictionary = new Dictionary(); private function itemOpenHandler(event:TreeEvent):void { var item:Object = event.item; if (item && item.@loadFlag == "false") { service.url = item.@url; var token:AsyncToken = service.send(); dictionary[token] = item; } } private function resultHandler(event:ResultEvent):void { var channel:XML = XML(dictionary[event.token]); var result:XML = XML(event.result); var items:XMLList = result..item; var item:XML = null; var node:XML = null; channel.@loadFlag = "true"; delete channel.*; for each (item in items) { node = <node/>; node.@label = item.title; channel.appendChild(node); } } private function faultHandler(event:FaultEvent):void { var channel:XML = XML(dictionary[event.token]); channel.@loadFlag = "true"; delete channel.*; } private function getLabel(item:Object):String { var node:XML = XML(item); if (node.name().toString() == "item") return node.title; else return node.@label; } ]]> </mx:Script> <mx:XML id="blog"> <root label="新浪博客"> <channel label="文化" url="http://rss.sina.com.cn/blog/index/cul.xml" loadFlag="false"> <node label="加载中..." /> </channel> <channel label="八卦" url="http://rss.sina.com.cn/blog/index/ent.xml" loadFlag="false"> <node label="加载中..." /> </channel> <channel label="情感" url="http://rss.sina.com.cn/blog/index/feel.xml" loadFlag="false"> <node label="加载中..." /> </channel> <channel label="观点" url="http://rss.sina.com.cn/blog/index/other.xml" loadFlag="false"> <node label="加载中..." /> </channel> <channel label="财经" url="http://rss.sina.com.cn/blog/index/stocks.xml" loadFlag="false"> <node label="加载中..." /> </channel> </root> </mx:XML> <mx:HTTPService id="service" concurrency="multiple" resultFormat="xml" result="resultHandler(event)" fault="faultHandler(event)" /> <mx:Tree id="tree" width="400" height="500" dataProvider="{blog}" labelField="@label" itemOpen="itemOpenHandler(event)" /> </mx:Application>
相关推荐
与卿画眉共浮生 2020-10-14
xiyang 2020-08-21
baijinswpu 2020-07-29
leonranri 2020-07-26
zhongliwen 2020-07-05
麋鹿麋鹿迷了路 2020-07-05
zengyu00 2020-07-05
XGQ 2020-07-04
CoderBoy 2020-06-28
whbing 2020-06-28
绝望的乐园 2020-06-27
wellfly 2020-06-26
菇星獨行 2020-06-25
草原孤狼 2020-06-25
坚持着执着 2020-06-16
wcqwcq 2020-06-14
yuanye0 2020-06-14
zhongliwen 2020-06-13
MrFuWen 2020-06-09