flex_(6)Rss_Xml_View示例;

=>RssViewerApp.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" 
  pageTitle="TheStudioOfCenyebao" minWidth="955" minHeight="600"
  creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import com.cen.programmingas3.RssXml.RssParser;

import mx.events.FlexEvent;

/*rssParser对象*/
private var rssParser:RssParser;

/**
* 初始化*/
protected function creationCompleteHandler(event:FlexEvent):void
{
rssParser = new RssParser();

rssParser.addEventListener("dataWritten", onDataReceived);
}

private function onDataReceived(event:Event):void
{
rssTitle.text =rssParser.rssTitle;
rssText.text = rssParser.rssOutput;
}
]]>
</fx:Script>
<!--从Internet加载 RSS数据-->
<s:VGroup width="650" height="350" verticalAlign="middle" verticalCenter="0" 
 horizontalAlign="center" horizontalCenter="0">
<s:Panel title="从 Internet 加载 RSS数据" width="100%" height="100%">
<s:VGroup width="100%" height="100%">
<s:Label id="rssTitle" fontSize="18" width="100%"/>
<s:TextArea id="rssText" width="100%" height="100%"/>
</s:VGroup>
</s:Panel>
</s:VGroup>
</s:Application>


=>RssParser .as

package com.cen.programmingas3.RssXml
{
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.*;

/**
* Rss解析类:
* 由RSS XML数据转换成HTML文本;
*/
public class RssParser extends EventDispatcher {

/**
* 属性*/
/*程序标题信息*/
public var sampleTitle:String = "Working with XML;";
/*程序描述信息*/
public var sampleDescription:String = "This example shows many concepts(概念).";

/*Rss数据源地址_"http://www.weather.gov/alerts/ak.rss"*/
//public var url:String = "http://weblogs.macromedia.com/product_feeds/archives/flex/index.rdf";
public var url:String = "public/data/RssViewerXml.rss";

/*rss标题信息*/
public var rssTitle:String;
/*Rss XML对象*/
public var rssXML:XML;
/* 输出字符串*/
public var rssOutput:String;
/*URLLoader对象*/
private var myLoader:URLLoader;
/*事件对象_标志数据已经处理完成*/
private var dataWritten:Event;

/**
* 构造函数
* Initiates loading of the RSS data.
*/
public function RssParser() {
/*URLRequest对象*/
var rssXmlUrl:URLRequest = new URLRequest(url);

myLoader = new URLLoader(rssXmlUrl);
myLoader.addEventListener("complete", xmlLoaded);
}

/** 
* Invoked(调用) when the RSS data is loaded. This method parses(解析) through the 
* XML data by looping through each item element in the XML, extracting(提取)
* the title,description and link elements in the item element. 
* The buildHTML() method returns HTML in the form of an XMLList
* object, which is converted to the rssOutput string.
* The channel.title property of the rssXML is used as the 
* title for the RSS feed. When the method is complete, it dispatches
* a dataWritten event, which notifies(通知) the host application of the
* completion.
*/
public function xmlLoaded(evtObj:Event):void { 
rssXML = XML(myLoader.data);

var outXML:XMLList = new XMLList();

if (rssXML.namespace("") != undefined) {
default xml namespace = rssXML.namespace("");
trace("=>rssXML.namespace_"+rssXML.namespace(""));
}

for each (var item:XML in rssXML..item) {
var itemTitle:String = item.title.toString();
var itemDescription:String = item.description.toString();
var itemLink:String = item.link.toString();

/**
* 把xml添加到xmlList对象上*/
outXML += buildItemHTML(itemTitle, 
itemDescription,
itemLink);
}

/*确定 toString()和 toXMLString()方法是否对某些标签之间的空白字符进行规格化;*/
XML.prettyPrinting = false;

/*输出字符串*/
rssOutput = outXML.toXMLString();
trace("=>rssOutput_"+rssOutput);

rssTitle = rssXML.channel.title.toString();

dataWritten = new Event("dataWritten", true);
dispatchEvent(dataWritten);
}
/**
* Builds an XMLList object that represents a segment(部分) of HTML code,
* based on the three string parameters that define the title, description,
* and link information from an RSS item.

* The return text is of the following form:
* <b>itemTitle</b>
* <p>itemDescription
* <br />
* <a href="link">
* <font color="#008000">More...</font>
* </a>
* </p> 
*/
private function buildItemHTML(itemTitle:String, 
  itemDescription:String,
  itemLink:String):XMLList {

default xml namespace = new Namespace();

var body:XMLList = new XMLList();
body += new XML("<b>" + itemTitle + "</b>");

var p:XML = new XML("<p>" + itemDescription + "</p>");

var link:XML = <a></a>;
link.@href = itemLink; // <link href="itemLinkString"></link>
link.font.@color = "#008000"; // <font color="#008000"></font></a> // 0x008000 = green;
link.font = "More...";

p.appendChild(<br/>); 
p.appendChild(link); 

body += p;
return body;
}
}
}


=>RssViewerXml.rss

<?xml version='1.0' encoding='UTF-8' ?>
<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/'>
    <channel>
        <title>
            Alaska - Current Watches, Warnings and Advisories for Alaska Issued by
            the National Weather Service
        </title>
        <link>
        http://www.weather.gov/alerts/ak.html
        </link>
        <lastBuildDate>
            Tue, 07 Feb 2006 17:28:01 EST
        </lastBuildDate>
        <ttl>
            4
        </ttl>
        <language>
            en-us
        </language>
        <managingEditor>
            robert.bunge@noaa.gov
        </managingEditor>
        <webMaster>
            w-nws.webmaster@noaa.gov
        </webMaster>
        <description>
            Current Watches, Warnings and Advisories for Alaska Issued by the National
            Weather Service
        </description>
        <image>
            <url>
                http://weather.gov/images/xml_logo.gif
            </url>
            <title>
                NOAA - National Weather Service
            </title>
            <link>
            http://weather.gov
            </link>
        </image>
        <item>
            <title>
                Short Term Forecast - Cape Fairweather to Cape Suckling Coastal Area (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ017.AYANOWYAK.221600
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T22:16:00 Expired At: 2006-02-08T04:00:00
                Issuing Weather Forecast Office Homepage: http://paya.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Taiya Inlet, Klondike Highway (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ018.AJKNOWAJK.212400
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:24:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Haines Borough, Lynn Canal (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ019.AJKNOWAJK.212400
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:24:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Glacier Bay (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ020.AJKNOWAJK.212400
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:24:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Glacier Bay (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ021.AJKNOWAJK.212400
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:24:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Cape Fairweather to Cape Suckling (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ022.AJKNOWAJK.212400
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:24:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Cape Decision to Cape Fairweather (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ023.AJKNOWAJK.212400
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:24:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Northern Lynn Canal (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ024.AJKNOWAJK.212400
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:24:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Juneau Borough, Western Chichagof, Admiralty Island
                (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ025.AJKNOWAJK.212400
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:24:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Inner Channels from Kupreanof Island to Etolin Island
                (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ026.AJKNOWAJK.212400
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:24:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Southern Inner Channels (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ027.ANTNOWANN.211600
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:16:00 Expired At: 2006-02-08T00:00:00
                Issuing Weather Forecast Office Homepage: http://pant.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Dixon Entrance to Cape Decision (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ028.ANTNOWANN.211600
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:16:00 Expired At: 2006-02-08T00:00:00
                Issuing Weather Forecast Office Homepage: http://pant.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Misty Fjords (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ029.ANTNOWANN.211600
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:16:00 Expired At: 2006-02-08T03:00:00
                Issuing Weather Forecast Office Homepage: http://pant.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Northeast Prince William Sound (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ131.AVWNOWVWS.215300
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T21:53:00 Expired At: 2006-02-08T03:00:00
                Issuing Weather Forecast Office Homepage: http://pavw.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Southern Seward Peninsula Coast (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ211.AOMNOWOME.192500
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T19:25:00 Expired At: 2006-02-08T01:00:00
                Issuing Weather Forecast Office Homepage: http://paom.arh.noaa.gov
            </description>
        </item>
        <item>
            <title>
                Short Term Forecast - Middle Tanana Valley (Alaska)
            </title>
            <link>
            http://www.weather.gov/alerts/ak.html#AKZ222.AFGNOWAFG.205700
            </link>
            <description>
                Short Term Forecast Issued At: 2006-02-07T20:57:00 Expired At: 2006-02-08T02:00:00
                Issuing Weather Forecast Office Homepage: http://pafg.arh.noaa.gov
            </description>
        </item>
    </channel>
</rss>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值