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.
