Parsing RSS 2.0 Feeds in ActionScript 3
mikechambers January 22nd, 2008
One of the ActionScript libraries that I use most is the as3syndicationlib. This is an opensource library originally developed by Christian Cantrell, and open sourced by Adobe. The library provides code for parsing RSS 1.0, RSS 2.0 and ATOM data feeds. It also provides a generic interface for parsing feeds when you do not know the format of the feeds.
Below is a simple example of how to use the library to parse an RSS 2.0 feed. The example is written in Flex 3 and ActionScript 3, although I have separated the code to make it easy to also use it within Flash CS3.
In order to compile and run the code below, you need to download the SWC (or source code) for the as3syndicationlib and corelib libraries (as3syndicationlib requires corelib to compile).
RSSExample.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script source="RSSExampleClass.as" />
<mx:TextArea left="20" top="10" bottom="40" right="10" id="outputField"/>
<mx:Button label="Load RSS" right="10" bottom="10" click="onLoadPress()"/>
</mx:Application>
RSSExampleClass.as
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);
}
The code for parsing RSS 1.0 and ATOM feeds is pretty much the same as the example above, except instead of using the RSS20 class, you will use the RSS10 or Atom10 classes.
Note, that full API documents for the library are included in the library download.
What do you do if you don’t know the feed type before you parse it? Well, that is where the library really shines, as it provides a FeedFactory class which can automatically detect and parse any supported feed types. I will try and post an example of using the FeedFactory soon.
You can download the as3syndicationlib library from here.






How does this work with the security sandboxing? I presume FeedBurner allows anyone to download their feeds?
Works the same as loading any other remote data. All crossdomain and security restrictions apply.
mike chambers
mesh@adobe.com
You may like the AMFPHP + MagpieRSS way I use to parses out RSS.
There are a few key reasons why I like to use a remote method for getting something as simple as an RSS feed.
1): No restrictions. No need to worry about crossdomain.xml. That has to be the one thing the stops people from using RSS in flash. There is A lot of RSS feeds out there “Like Netflix” that don’t provide a crossdomain.xml file requiring you to use some form of proxie to get around it. This can also be worked around by using Yahoo’s pipes as a kinda proxie for getting around crossdomain problems
2): Scalability. If you know much about php for this example you could use Magpie’s Integrated Object Caching or leverage the power of php to make use of something more then just a RSS feed only.
3): Usability. This is very simple to impalement, though you may need some prior knowledge of php and flash remoting. Once installed you can make a remote call from flash and pass it the url and get an array back ready to use any way you want.
I made a mini tutorial at http://gfxcomplex.com/blog/amfphp/overcome-flash-and-rss-crossdomain-problems-with-amfphp-and-magpie/
Very usefull example thanks for posting, nice and short
[...] Chambers shows how to use the as3syndicationlib actionscript library to parse RSS 2.0 Feeds in ActionScript 3. Tags: Actionscript, Atom, Mike Chambers, [...]
Great library. A couple things to note:
Despite it being called as3syndicationlib and not mentioning flex, it actually has references to a few flex classes and won’t work in an actionscript only project unless you modify the classes and replace these references.
Its missing a few popular modules namely the Yahoo Media module that most media syndication feeds use.
I wish the framework was a bit simpler or dynamic in incorporating additional modules as right now its a fairly intricate process.
Adobe has released a sample Flex 3 app called Media Widget and the company that developed it, Teknision, used this library for its feed parsing. They’ve modified it adding the Yahoo Media module among other improvements and this updated source is available in Media Wiget source code. The “official” project code hasn’t been updated in over a year and doesn’t reflect these changes.
Overall a class not to live without for any syndication parsing project :)
>Great library. A couple things to note:
Well, it is an open source community project, so if anyone would like to add that, or other functionality, just let me know.
mike chambers
mesh@adobe.com
I wouldn’t mind adding some of the things I noted when I find the time but the main thing I wanted to mention in that blob of text is that an updated version of the library is accessible in the Media Widget sample code with flex 3 that has more media feed parsing functionality and namespaces built in, until the library itself is updated–might save someone some time.
More on Parsing Feeds with Flex/AS3
Several days ago I posted a couple lines of code that came in quite handy while parsing an Atom feed. Mike Chambers has put together a more in depth post on parsing Atom/RSS feeds using the as3syndica…
Cant the data be requested using JS nad passed on to the swf for parsing to overcome the crossdomain.xml restrictions?
Nice example. How do you take care of media-rss etc? I have to hack as3syndicationlib and add those media-rss capabilities.
It would be great to see an example that shows extending as3syndicationlib for various extentions (creativeCommons, a9, media-rss etc).
Thanks
-abdul
[...] Parsing RSS Feed January 27, 2008 — drawk Mike Chambers has a great post on parsing RSS in AS3 with as3syndicationlib. I had some info on this but this feed is very simple and just posting the [...]
>Cant the data be requested using JS nad passed on to the swf for parsing to overcome the crossdomain.xml restrictions?
Yes, although JS crossdomain restrictions are much stricter than Flash’s (Flash has a permission based way around them via crossdomain.xml).
mike chambers
mesh@adobe.com
>How do you take care of media-rss etc? I have to hack as3syndicationlib and add those media-rss capabilities.
Ill try and see if I can get Christian to respond, but you can grab individual raw items from the feed in order to extend the parser.
mike chambers
mesh@adobe.com
Hello,
I’m trying to get your example to work with Flash CS3 but I ran into a problem.
ReferenceError: Error #1065: Variable mx.utils::StringUtil is not defined.
at com.adobe.xml.syndication::ParsingTools$/nullCheck()
at com.adobe.xml.syndication.rss::RSS20/get items()
at main/parseRSS()
at main/onDataLoad()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
In ParsingTools.as I found:
import mx.utils.StringUtil;
This class [mx.utils.StringUtil] seems to be part of the Flex library and not in Flash CS3, any suggestions on how I can get this to work?
Make sure to link in the corelib library:
http://code.google.com/p/as3corelib/
mike chambers
mesh@adobe.com
I am importing the as3corelib package and can reference them. From what I can tell all the packages in as3corelib are packaged as com.adobe.[name].[name] But in ParsingTools.as it imports mx.utils.StringUtil - different package name. To test I wasn’t doing anything too crazy I created a new flash file and tried this.
———————
import mx.utils.StringUtil;
var s:String =”adfadfaf”;
trace(StringUtil.trim(s).length == 0)
———————
the result was this:::
ReferenceError: Error #1065: Variable mx.utils::StringUtil is not defined.
at testUtil_fla::MainTimeline/frame1()
———————
Are you able to get it to work using Flash CS3?
>mx.utils.StringUtil
Ahh, actually, that is the Flex String utils. I just checked the code, and if you change the import from:
mx.utils.StringUtil
to
com.adobe.utils.StringUtil
It should work (the second is the StringUtil in corelib).
I try and update the library with this fix soon.
mike chambers
mesh@adobe.com
That did the trick.
Thanks!
Hello,
Could you provide a sample of how to retrieve “media” from an RSS20Item. I’ve been trying but the media property always returns null. Other properties such as link or id do return values.
Thanks
Hello, i ran into an issue with the DateBase class and Flash CS3. I think it’s the mx.formatters.DateBase classe from Flex i think.
It’s not available in the as3corelib but it is accessed threw syndication lib. Is there a workaround similar to the StringUtil class?
DateBase class is used by the DateUtil class from the as3corelib (com.adobe.util - package).
thanks, hannes
Same issue as Hannes.
DateUtil.as imports mx.formatters.DateBase which doesn’t appear to be a class of either the syndication or corelib packages.
Steve
I made this little class that I put in ‘com.adobe.utils’, which contains JUST ENOUGH to get the syndication API going. You can get the source here:
http://www.newcommerce.ca/as3/DateBase.as
Simply change the instances of:
import mx.utils.DateBase;
by:
import com.adobe.utils.DateBase;
BTW: Somebody might want to doublecheck this, as I’m not 100% sure it’s correct.
:)
Thank you Martin!
Mike: Once I made that substitution and tried again to compile, the mx.utils.StringUtil class was requested, but when I changed that to com.adobe.utils.StringUtil it finally compiled and worked. I really appreciate your help Martin, and Mike I really appreciate this information.
Steve
Mike,
Thanks for the great tutorial. Your code made it easy to get a jump start on working with the library. This post inspired me to build a simple app that uses a Yahoo Pipes RSS feed (combining snap previews with digg stories). I ended up using a php caching script to get around crossdomain issues.
Here is the demo w/source:
http://infinitearray.com/blog/2008/02/yahoo-pipes-flex.html
I’ve extended the as3syndicationlib so that you can use it with Media RSS. There are about ten new classes that represent all the media rss elements. All Media RSS tags must be within the “media:content” tag, groups and more than one element of the same tag are not supported at the moment.
You can download the very first version in the comments of this post:
http://www.video-flash.de/index.php/as3syndicationlib-rss-und-atom-feeds-mit-actionscript-3-parsen/#comments
I’m getting errors when importing the SWC (I have Corelib SWC imported already). I narrowed down the bug to be in the RSS20.
If I just create a RSS20 object I get two errors:
Unable to resolve resource bundle “formatters” for locale “en_US”.
Unable to resolve resource bundle “SharedResources” for locale “en_US”.
sorry, i can’t find your email, so i’m posting it here.
i think you’ve been plagiarized…
http://www.elokcomputer.web.id/2008/02/09/parsing-rss-20-feeds-in-actionscript-3/
Hi,
Just wanted to remind you about this:
http://www.mikechambers.com/blog/2008/01/22/parsing-rss-20-feeds-in-actionscript-3/#comment-10994
I had the same issue.
Thanks
Awesome! I’m using this with Yahoo Pipes (to combat cross-domain issues) to build a feed reader. It works a darn treat! Cheers Mike!
@Ed - I’m trying to use Pipes in the very same way but still receiving crossdomain security errors (SecurityError: #2048).
Would you mind explaining how you are able to use Pipes to bypass Flash’s built-in crossdomain enforcement?
Thanks.
- Chris
Chris, the way I did it was to set up a simple pipe to parse a feed of the user’s choosing, using the Pipes url input and fetch modules. I then set up Flash to parse the resulting Pipes feed address (using the as3syndicationlib) along with the request url of the feed address. Oh, and you have to use the yahooapis.com domain for your request URL as that’s where the vital crossdomain.xml policy file lives on Yahoo’s servers. So your request URL should look something like this (replace the capital letters and hyphens accordingly):
http://pipes.yahooapis.com/pipes/pipe.run?_id=PIPE-ID&_render=rss&NAME-OF-URL-INPUT-MODULE=http://URL-OF-FEED
Hope that helps!
Ed.
Ed,
Great - thanks! That is a vital piece of information that I didn’t find on Yahoo! Pipes. I need to look better next time.
I need to look at the API and see if it’s possible to add feeds to a pipe progmatticaly as that would be sweet.
Thanks.
Ed,
Pardon me but do you know if it possible to bypass the need for a pipe - or - to simply isolate a single RSS feed URL and access it directly through yahooapis.com/pipes?
I see what might be a snippet of that in your “&NAME-OF-URL-INPUT-MODULE”.
Basically, I have a list of feeds stored in our Oracle database that the user is able to customize. When selecting one of those feeds, I’m currently use DBMS_XMLQuery to extract the values using HTTPURITYPE.getXML(). The problem with this approach is that my XML output doesn’t come back with an header, therefore Adobe Flex is kinda like “Huh?” when it sees the output.
Since I can’t find an approach to inserting an feed directly into a Pipe (thinking about it, that would be pretty hard to do since the user has to connect feeds to outputs, etc.); I was thinking that might be able to access an RSS url through Yahoo Pipes without the need for having that feed inside of an already created pipe.
Does that make sense? Look forward to seeing your response.
Thanks.
Hello everyone.
Trying to parse the following feeds, I get an “Unable to determine feed type”!
http://www.heise.de/security/news/news.rdf
http://www.heise.de/newsticker/heise.rdf
Why? I think this is normal rss 1.0…
Thanks for any suggestions!
Greetings,
Tim.
How can this library be compiled/ build???
Is there a way doing it using Flex 2.0 on WinXP?
Greetings and thanks,
Tim.
I would want to use this in Flash CS3 (no Flex). Any chance someone could give me (and others) some points as to how to do that? I’ve tried and not getting anywhere…
[...] suposed to be informative and quick. In regards to use the FlashDevelop IDE and it is based off of Mike Chambers [...]
This solution pared with Yahoo! Pipes worked like a charm. Good stuff, good stuff.
Hi, I used the library in my application to read RSS feeds but for some reason it only works in IE and not Firefox??? When the Firefox browser begins to load the RSS, it will display “Transferring data from …” or “Reading RSS …” but it just stays like that forever. Nothing actually happens. Please help if you can. Thanks in advance.
*** For those wanting to use this stuff in Flash CS3… ***
I too came up against the problem of certain classes failing to import in Flash (the mx ones)… but with some good old fashioned digging, I found that the old “mx.formatters.DateBase” class was really close to a class included by Adobe in their “Programming Actionscript 3″ examples… I added a couple of static arrays for month and day names and it started to work. In addition, there were just some problems with things like “StringUtils” being moved into different packages with the porting to AS3, so I updated the “import” directives to take care of that.
The bottom line is “IT ALL WORKS” - based on Mike’s code above, I have uploaded a ZIP of the above work, (including the corelib classes) at http://www.g-raff.com/downloads/syndicationExample.zip - maybe someone can assist me in letting me know the correct protocol for adding this new ported “DateBase” class to the corelib?
Anyway - this example is specifically for users of Flash - a syndication example that just does exactly what Mike is doing above, but including the FLA and a little SyndicationExample.as file that I just wrote - its basically a template method so you can take it from there. In addition, my file takes the URL of the feed as a single argument, so you are not restricted to one feed… you’ll see when you open the files.
Thanks for the starting point, Mike.
Steve jones
Hi Mike!
Thanks for the examples and classes. I just submitted a patch to the library for GeoRSS support. It’d be great to see it integrated, as I’m going to be recommending people use this framework in the Flasj Maps API for parsing GeoRSS feeds.
- pamela
In this tutorial by Lee Brimelow, he showcases how to parse XML with the kuler namespace. http://www.gotoandlearn.com/player.php?id=65
I am just wondering if it POSSIBLE to parse mediaRSS without the use of all these libraries, just using the techniques shown by Lee Brimelow?