RSS/Atom syndication made easy

A while ago adobe released their as3syndicationlib for easy access to RSS & Atom feeds by converting either RSS 1.0/2.0 or Atom feeds into generic data accessible via the IFeed interface. These libraries also require adobe's corelib.

It literally took me 5 minutes to write a simple wrapper class to handle the loading of the feeds, and simply trace out the headlines of each item.

Actionscript:
  1. package app.feeds
  2. {
  3.     import com.adobe.xml.syndication.generic.FeedFactory;
  4.     import com.adobe.xml.syndication.generic.IFeed;
  5.     import com.adobe.xml.syndication.generic.IItem;
  6.    
  7.     import flash.events.Event;
  8.     import flash.net.URLLoader;
  9.     import flash.net.URLRequest;
  10.    
  11.     public class FeedLoader
  12.     {
  13.         private var loader:URLLoader;
  14.        
  15.         public var feed:IFeed;
  16.        
  17.         public function FeedLoader( feedURL:String )
  18.         {
  19.             loader = new URLLoader();
  20.             loader.addEventListener( Event.COMPLETE, loadComplete );
  21.             loader.load( new URLRequest( feedURL ) );
  22.         }
  23.        
  24.         private function loadComplete( e:Event ):void
  25.         {
  26.             feed = FeedFactory.getFeedByXML( new XML( e.target.data ) );
  27.            
  28.             var i:uint;
  29.             for( i=0; i<feed.items.length; i++ )
  30.             {
  31.                 trace( IItem( feed.items[ i ] ).title );
  32.             }
  33.         }
  34.     }
  35. }

To implement:

Actionscript:
  1. var feed1:FeedLoader = new FeedLoader( 'http://www.guardian.co.uk/rss' );

If you're using flash as I am, you may find that it spits the dummy when it tries to load mx.utils.StringUtil, you'll need to add an ActionScript classpath pointing to the flex framework, which on my machine was in "/Applications/Adobe Flex Builder 3/sdks/2.0.1/frameworks/source"

5 Comments so far

  1. Justin Turner / July 20th, 2007 9:44 pm

    Thanks a lot. I like your work. The Polar Clock is my home page. - Justin Turner, C2 Global Technolgoies

  2. Rob / October 18th, 2007 7:11 am

    Of course I found your tutorial AFTER spending time going through all the classes and locating/copying/pasting the Flax classes into my classpath.

    I've done my initial tests and I was curious if you think there is an advantage to using the IFeed interface and FeedeFactory classes, other than format independence? The brief documentation led me to believe ther might be problems if one is not specific, so I ended up doing this:

    feed = new Atom10();
    feed.parse(e.target.data);
    trace(feed.entries[0].content.value);

    Granted, this is pretty specific. Does your method of using the interfaces actually allow greater flexibility? Is it the way the developers intended us to use it?

    Thanks for your post!

  3. Neil / November 24th, 2007 7:56 am

    works great, thanks! i'm having a little bit of trouble taking the feed information and incorporating it into the mxml file rather than the trace statement in the AS file. Everytime I try I get a NULL value in the object, any tips would be much appreciated.

  4. izleara / November 29th, 2007 10:08 pm

    thank you.

  5. Ryan / January 29th, 2008 2:03 am

    I copied the com directories for both the as3syndicationlib and adobe's corelib to my project's directory. When I tried to use your class I get the error "1172: Definition mx.utils:StringUtil could not be found." Is there something more I need to do?

Leave a reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image