AS3 Parsing RSS Feed
January 27, 2008 — drawkMike 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.
March 9, 2008 at 2:00 pm
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!
March 9, 2008 at 2:19 pm
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.
March 9, 2008 at 2:21 pm
btw making a new language highlighter is a matter of adding the keywords from the language.
March 23, 2008 at 11:31 am
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.)
March 24, 2008 at 10:18 am
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.
May 7, 2008 at 10:18 am
How could I use this in a Flash CS3 project?
May 11, 2008 at 12:55 pm
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.