AS3 Parsing RSS Feed

Mike Chambers has a great post on parsing RSS in AS3 with as3syndicationlib. I had some info on this in development but Mike’s post is very simple and just posting the code here for reference.

import com.adobe.utils.XMLUtil;
import com.adobe.xml.syndication.rss.Item20;
import com.adobe.xml.syndication.rss.RSS20;

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;

private var loader:URLLoader;

//url of rss 2.0 feed
private static const RSS_URL:String = "http://feeds.feedburner.com/MikeChambers/";

//called when user presses the button to load feed
private function onLoadPress():void
{
 loader = new URLLoader();

	//request pointing to feed
 var request:URLRequest = new URLRequest(RSS_URL);
 request.method = URLRequestMethod.GET;

	//listen for when the data loads
 loader.addEventListener(Event.COMPLETE, onDataLoad);

	//listen for error events
 loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);

	//load the feed data
 loader.load(request);
}

//called once the data has loaded from the feed
private function onDataLoad(e:Event):void
{
 //get the raw string data from the feed
 var rawRSS:String = URLLoader(e.target).data;

	//parse it as RSS
 parseRSS(rawRSS);

}

//parses RSS 2.0 feed and prints out the feed titles into
//the text area
private function parseRSS(data:String):void
{

	//XMLSyndicationLibrary does not validate that the data contains valid
 //XML, so you need to validate that the data is valid XML.
 //We use the XMLUtil.isValidXML API from the corelib library.
 if(!XMLUtil.isValidXML(data))
 {
 	writeOutput("Feed does not contain valid XML.");
 	return;
 }	

	//create RSS20 instance
 var rss:RSS20 = new RSS20();

		//parse the raw rss data
 	rss.parse(data);

		//get all of the items within the feed
 	var items:Array = rss.items;

		//loop through each item in the feed
 	for each(var item:Item20 in items)
 	{
 		//print out the title of each item
 		writeOutput(item.title);
 	}
}

private function writeOutput(data:String):void
{
 outputField.text += data + "\n";
}

private function onIOError(e:IOErrorEvent):void
{
 writeOutput("IOError : " + e.text);
}

private function onSecurityError(e:SecurityErrorEvent):void
{
 writeOutput("SecurityError : " + e.text);
}

Check out the full post on this at Mike’s blog.

Advertisements

7 Responses to “AS3 Parsing RSS Feed”

  1. xleon Says:

    Hi, first of all, Thanks for your blog and good information.
    I´d like to know where I can find the syntax hightlight you´re using. I have been using some of them but I like yours much more.

    Thanks again!

  2. drawk Says:

    http://code.google.com/p/syntaxhighlighter/

    Google syntax highlighter. I just use the jscript markup for as but there are just loads and loads of good ones out there. Most of them you just markeup the PRE tag with code and lang.

  3. drawk Says:

    btw making a new language highlighter is a matter of adding the keywords from the language.

  4. Jeremy Says:

    I have a stupid question, but I can’t seem to find the answer anywhere.

    How do I install as3syndicationlib? I know it needs corelib, but how do you install that? Is there a folder I’m suppose to be putting these packages? Thanks.

    (btw, I agree, that is pretty cool syntax highlighting. I’ve added it to some of my own stuff.)

  5. drawk Says:

    http://code.google.com/p/as3corelib/

    Everything is put in the root folder of your project with AS3 or you can set a reference path in the IDE you are using.

  6. Magnus Says:

    How could I use this in a Flash CS3 project?

  7. drawk Says:

    Hey magnus,

    Yeh I believe part of the as3corelib references soem flex mx classes. I will see if I can’t update this page to include flash specific parsing soon.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: