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.

43 Responses to “Parsing RSS 2.0 Feeds in ActionScript 3”

  1. Mark Ingramon 22 Jan 2008 at 1:48 pm

    How does this work with the security sandboxing? I presume FeedBurner allows anyone to download their feeds?

  2. mikechamberson 22 Jan 2008 at 1:54 pm

    Works the same as loading any other remote data. All crossdomain and security restrictions apply.

    mike chambers

    mesh@adobe.com

  3. Josh Chernoffon 22 Jan 2008 at 4:37 pm

    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/

  4. julienon 24 Jan 2008 at 3:55 am

    Very usefull example thanks for posting, nice and short

  5. [...] Chambers shows how to use the as3syndicationlib actionscript library to parse RSS 2.0 Feeds in ActionScript 3. Tags: Actionscript, Atom, Mike Chambers, [...]

  6. Markon 24 Jan 2008 at 8:57 am

    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 :)

  7. mikechamberson 24 Jan 2008 at 9:57 am

    >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

  8. Markon 24 Jan 2008 at 12:22 pm

    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.

  9. T3Bon 26 Jan 2008 at 5:42 pm

    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…

  10. PL/SQLon 26 Jan 2008 at 7:23 pm

    Cant the data be requested using JS nad passed on to the swf for parsing to overcome the crossdomain.xml restrictions?

  11. Abdul Qabizon 27 Jan 2008 at 8:52 am

    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

  12. AS3 Parsing RSS Feed « [ draw.logic ]on 27 Jan 2008 at 7:41 pm

    [...] 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 [...]

  13. mikechamberson 28 Jan 2008 at 10:07 am

    >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

  14. mikechamberson 28 Jan 2008 at 10:09 am

    >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

  15. Daveon 30 Jan 2008 at 6:42 am

    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?

  16. mikechamberson 30 Jan 2008 at 9:00 am

    Make sure to link in the corelib library:

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

    mike chambers

    mesh@adobe.com

  17. Daveon 30 Jan 2008 at 12:17 pm

    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?

  18. mikechamberson 30 Jan 2008 at 12:32 pm

    >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

  19. Daveon 30 Jan 2008 at 12:38 pm

    That did the trick.

    Thanks!

  20. Daveon 31 Jan 2008 at 3:40 pm

    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

  21. Hannes Moseron 05 Feb 2008 at 4:14 pm

    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

  22. Steve Nelsonon 06 Feb 2008 at 2:35 pm

    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.

  23. Martin Legrison 08 Feb 2008 at 11:59 am

    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.

    :)

  24. Steve Nelsonon 09 Feb 2008 at 2:19 pm

    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

  25. Ryan Robinsonon 10 Feb 2008 at 6:00 pm

    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

  26. Floon 20 Feb 2008 at 2:46 pm

    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

  27. Dannyon 22 Feb 2008 at 7:27 am

    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”.

  28. Tedon 09 Mar 2008 at 6:52 pm

    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/

  29. Adam Bowmanon 16 Mar 2008 at 8:06 am

    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

  30. Edon 18 Mar 2008 at 10:42 am

    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!

  31. Chrison 25 Mar 2008 at 12:30 pm

    @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

  32. Edon 26 Mar 2008 at 11:16 am

    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.

  33. Chrison 31 Mar 2008 at 12:29 pm

    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.

  34. Chrison 31 Mar 2008 at 1:03 pm

    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.

  35. Timon 21 Apr 2008 at 2:02 am

    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.

  36. Timon 21 Apr 2008 at 7:04 am

    How can this library be compiled/ build???
    Is there a way doing it using Flex 2.0 on WinXP?

    Greetings and thanks,
    Tim.

  37. Magnuson 07 May 2008 at 9:16 am

    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…

  38. [...] suposed to be informative and quick. In regards to use the FlashDevelop IDE and it is based off of Mike Chambers [...]

  39. Johnon 27 May 2008 at 2:42 pm

    This solution pared with Yahoo! Pipes worked like a charm. Good stuff, good stuff.

  40. Samon 28 May 2008 at 10:01 am

    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.

  41. Steve Joneson 31 May 2008 at 1:26 am

    *** 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

  42. Pamela Foxon 04 Jun 2008 at 1:53 pm

    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

  43. Marcuson 23 Jun 2008 at 12:46 am

    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?

Trackback URI | Comments RSS

Leave a Reply