Actionscript 3 Designer Toolkit

More design, less programming

Actionscript 3 Designer Toolkit header image 2

Parsing and Displaying a RSS feed in Actionscript 3

December 13th, 2009 · No Comments · Tutorials

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.

rss_item

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 (71)

If you found this post helpful please consider a Retweet.  Thanks…

Post to Twitter

Tags: ··

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment

Spam Protection by WP-SpamFree