View comments | RSS feed

Example: Loading RSS data from the Internet

The RSSViewer sample application shows a number of features of working with XML in ActionScript, including the following:

The RSS format is widely used to syndicate news via XML. A simple RSS data file may look like the following:

<?xml version="1.0" encoding="UTF-8" ?> 
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
    <title>Alaska - Weather</title> 
    <link>http://www.nws.noaa.gov/alerts/ak.html</link> 
    <description>Alaska - Watches, Warnings and Advisories</description> 

    <item>
        <title>
            Short Term Forecast - Taiya Inlet, Klondike Highway (Alaska)
        </title> 
          <link>
            http://www.nws.noaa.gov/alerts/ak.html#A18.AJKNK.1900
        </link> 
          <description>
            Short Term Forecast Issued At: 2005-04-11T19:00:00 
            Expired At: 2005-04-12T01:00:00 Issuing Weather Forecast Office 
            Homepage: http://pajk.arh.noaa.gov
        </description> 
  </item>
    <item>
          <title>
            Short Term Forecast - Haines Borough (Alaska)
        </title> 
         <link>
            http://www.nws.noaa.gov/alerts/ak.html#AKZ019.AJKNOWAJK.190000
        </link>
          <description>
            Short Term Forecast Issued At: 2005-04-11T19:00:00 
            Expired At: 2005-04-12T01:00:00 Issuing Weather Forecast Office 
            Homepage: http://pajk.arh.noaa.gov
        </description> 
    </item>
</channel>
</rss>

The SimpleRSS application reads RSS data from the Internet, parses the data for headlines (titles), links, and descriptions, and returns that data. The SimpleRSSUI class provides the UI and calls the SimpleRSS class, which does all of the XML processing.

To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The RSSViewer application files can be found in the folder Samples/RSSViewer. The application consists of the following files:

File

Description

RSSViewer.mxml

or

RSSViewer.fla

The main application file in Flash (FLA) or Flex (MXML).

com/example/programmingas3/rssViewer/RSSParser.as

A class that contains methods that use E4X to traverse RSS (XML) data and generate a corresponding HTML representation.

RSSData/ak.rss

A sample RSS file. The application is set up to read RSS data from the web, at a Flex RSS feed hosted by Adobe. However, you can easily change the application to read RSS data from this document, which uses a slightly different schema than that of the Flex RSS feed.

Subtopics

Reading and parsing XML data
Assembling XMLList data
Extracting the title of the RSS feed and sending a custom event

Reading and parsing XML data

The RSSParser class includes an xmlLoaded() method that converts the input RSS data, stored in the rssXML variable, into an string containing HTML-formatted output, rssOutput.

Near the beginning of the method, code sets the default XML namespace if the source RSS data includes a default namespace:

if (rssXML.namespace("") != undefined)
{
    default xml namespace = rssXML.namespace("");
}

The next lines then loop through the contents of the source XML data, examining each descendant property named item:

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();
    outXML += buildItemHTML(itemTitle, 
                            itemDescription,
                            itemLink);
}

The first three lines simply set string variables to represent the title, description and link properties of the item property of the XML data. The next line then calls the buildItemHTML() method to get HTML data in the form of an XMLList object, using the three new string variables as parameters.

Assembling XMLList data

The HTML data (an XMLList object) is of the following form:

<b>itemTitle</b>
<p>
    itemDescription
    <br />
    <a href="link">
        <font color="#008000">More...</font>
    </a>
</p>

The first lines of the method clear the default xml namespace:

default xml namespace = new Namespace();

The default xml namespace directive has function block-level scope. This means that the scope of this declaration is the buildItemHTML() method.

The lines that follow assemble the XMLList, based on the string arguments passed to the function:

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;

This XMLList object represents string data suitable for an ActionScript HTML text field.

The xmlLoaded() method uses the return value of the buildItemHTML() method and converts it to a string:

XML.prettyPrinting = false;
rssOutput = outXML.toXMLString();    

Extracting the title of the RSS feed and sending a custom event

The xmlLoaded() method sets a rssTitle string variable, based on information in the source RSS XML data:

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

Finally, the xmlLoaded() method generates an event, which notifies the application that the data is parsed and available:

            dataWritten = new Event("dataWritten", true);

Flash CS3


Comments


No screen name said on Jan 9, 2008 at 12:39 AM :
How do you add the correct URL to button when parsing RSS feeds with AS 3.0?

I'm creating 10 movieclips in my flash movie, one for each RSS item. In each of this movieclips I add the title of the RSS item and this title is clickable by a button to navigate to the corresponding link of the RSS item.

If I parse the RSS feed and I add the navigateToUrl function to the button, I get the link from the first RSS item on every button, although each movieclip has the correct title.

Can someone help me out with the correct code for this?

I've spent a lot of time to find a good example of an AS 3.0 RSS parser that creates different movieclips from the different items of an RSS feed with linked buttons, but I can't find one that tells me how to put the correct link on the different buttons.

Thanks a lot.

Guykes
eliasfarah said on Feb 20, 2008 at 1:18 PM :
HI,

I ve downloaded the RSSViewer example, it works very well on my PC.
But once i uploaded to a hosting server it didn't work.

Kindly, can anybody help me with this issue??

you can find the problem at www.metfar.ca/RSS.

Thanks
Elias,
eliasfarah@yahoo.com
1-514-833-6696

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000133.html