This post decribes the code required to download a RSS feed and display the reformatted XML data onscreen. The code relies on the HTMLTextMultiline class to display the processed data.

The HTMLTextMultiline class works well in this situation since it automatically – without any additional programming – handles the image tags that are included in this feed’s description nodes. Not much more to explain as code is pretty well commented. Download includes code below and HTMLTextMultiline class.
// import dtk class import com.dtk.HTMLTextMultiline; // url of rss feed var feedAddress:String = 'http://feeds.feedburner.com/TechCrunch'; // range of nodes requested var firstNode:uint = 3; var lastNode:uint = 4; // url loader object var rssLoader:URLLoader = new URLLoader(); // url request object loaded with feed address var rssURL:URLRequest = new URLRequest( feedAddress ); // add event listener to signal when loading is completed rssLoader.addEventListener(Event.COMPLETE, rssLoaded); // load feed rssLoader.load( rssURL ); // function initiated by COMPLETE event function rssLoaded(evt:Event):void { // instance of XML object var rssXML:XML = new XML(); // fill width XML data passed from url rssXML = XML(rssLoader.data); // instance of XMLList object var rssXMLList:XMLList = rssXML.channel.item; // init y starting position var yPos:uint = 20; /** * to get the entire feed downloaded, substitute this for statement for the one below: * * for ( i:uint=0; i<rssXML.channel.item.length(); i++ ) */ // loop through range of nodes specified for ( var i:uint=firstNode; i<=lastNode; i++ ) { /** * process XML data into display format */ // wrap title in HTML bold tags var rssTitle:String = '<b>' + rssXML.channel.item[i].title + '</b>'; // to convert to linked title wrap in HTML link tags & underline var linkedTitle:String = "<a href='" + rssXML.channel.item[i].link + "'><u>" + rssTitle + "</u></a>"; /** * presentation of data using HTMLTextMultiline classes */ // sprite parent to each rss item var entry:Sprite = new Sprite(); addChild( entry ); // display linked title with instance of HTMLTextMultiline class var tt:HTMLTextMultiline = new HTMLTextMultiline( 340, 'Verdana', 12, 0x333333, 'LEFT', 0, linkedTitle ); tt.x = 10; tt.y = yPos; entry.addChild( tt ); // display publish date var dt:HTMLTextMultiline = new HTMLTextMultiline( 110, 'Verdana', 11, 0x333333, 'LEFT', 0, rssXML.channel.item[i].pubDate.substr(0, 16) ); dt.x = 350; dt.y = yPos; entry.addChild( dt ); // increment y position by sprite height which is determined by textfield height yPos += entry.height; // display description which will automatically display any images included with another instance of HTMLTextMultiline class var ml:HTMLTextMultiline = new HTMLTextMultiline( 470, 'Verdana', 12, 0x333333, 'LEFT', 0, rssXML.channel.item[i].description ); ml.x = 10; ml.y = yPos; entry.addChild( ml ); // get description textfield height (I reduced by 20) var entryHeight = entry.height - 20; // increment to starting y position of next item yPos += entryHeight; } } |
Download files: rss_reader.zip (173)
If you found this post helpful please consider a Retweet. Thanks…
Hello,
This is the best example I’ve seen, so far, for parsing RSS feed in ActionScript. Thank very much. I downloaded rss_reader.zip, but when I tried to open the rss_reader.fla an error pops up that reads, “Unexpected file format.” So, I copied and pasted your above code. When I plugged in the RSS url that I need I get: TypeError: Error #1010: A term is undefined and has no properties.
Any thoughts?
Thanks, this post is incredibly helpful!!!
You, sir, are a life saver. I needed something that could deal with tags that are already embedded into the description tag of a feed, and that’s exactly what your sample code does.
Thanks!