异步加载新浪博客的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>
 

相关推荐