Flash RSS Reader Pg.9

source: http://www.thegoldenmean.com

9 — Completing the Parsing Class

Putting Theory into Practice

Finally - a page with a little class…

We have spent a lot of time considering how to approach consuming an RSS document. Now it’s time to build a real Flash application using some of what we have learned.

I am going to use the XPath approach discussed on the previous page. Our Reader is going to be a mixture of Object Oriented and procedural (timeline) code. It seemed efficient to write a class to consume the RSS feed, and to put the navigation and visual formatting code in the movie itself. Let’s get down to business by starting with the preliminaries.

Setting up the environment

That’s it for preliminaries. I am now going to present the whole class in one lump so you can skim it to get a sense of how things work. We will then examine it piece by piece.

ProcessRss Class

import com.xfactorstudio.xml.xpath.*;
import mx.utils.Delegate;
import mx.controls.TextArea;

class ProcessRss {

//declare private properties
private var titles:Array;
private var links:Array;
private var descriptions:Array;
private var _xml:XML;
private var proxyPath:String;
private var senderObj:LoadVars;
private var loaderID:Number;

//declare public properties
public var targetClip:MovieClip;
public var textTarget:TextArea;

function ProcessRss (_mc:MovieClip, _ta:TextArea, proxy:String) {
  targetClip = _mc;
  textTarget = _ta;
  proxyPath = proxy;
  _xml = new XML();
  _xml.ignoreWhite = true;  
  _xml.onLoad = Delegate.create(this, processFeed);

//loadFeed method accepts one argument: the URL of the feed to load
public function loadFeed(url:String):Void {
  //create new LoadVars Object
  senderObj = new LoadVars();
  //declare a property of this Object and assign it the URL value
  senderObj.rss = url;
  senderObj.sendAndLoad(proxyPath, _xml, "POST");

private function processFeed():Void {
  titles = XPath.selectNodes(_xml, "//item/title/text()");
  links = XPath.selectNodes(_xml, "//item/link/text()");
  descriptions = XPath.selectNodes(_xml, "//item/description/text()");
  //once arrrays have been filled, format and display the information
  //free memory by emptying arrays
  titles = [];
  links = [];
  descriptions = [];

private function displayFeed():Void {
  //reset scroll position of TextArea component
  textTarget.vPosition = 0;
  var itemNum:Number = titles.length;
  //loop through arrays and send formatted strings to TextArea
  for (var i:Number = 0; i<itemNum; i++) {
    textTarget.text += "<headline><a href='"+
          links[i]+"' target='_blank'>"+titles[i]+


Code Dissection

We begin by importing some external code. Importing the XPath class was covered on the previous pages. We import the TextArea class so we can call its methods without having to type fully qualified references which is tiresome. Delegate is a new class introduced in the Flash 7.2 upgrade (“Elipsis”) and is most helpful for dealing with scope in classes. Delegate is used in the ProcessRss class when the XML method onLoad() is invoked. (Helpful links for additional reading about Delegate may be found on page Thirteen.)

Next comes the class announcement and all variables are declared. None of the variables are assigned any values at this point but because they have been declared as instance variables they can be referred to within the class.

Next up is the constructor function. Note the arguments it expects to get: the target move clip (important for establishing scope and avoiding timeline collisions if this is used as a loaded movie in another container movie), the TextArea component which will display the text and the path to the proxy script.

In the constructor we declare a new XML object and assign a function to be executed when the XML Object loads. Note the use of Delegate which insures that the function will operate in the scope of the class, not the XML object.

The class’ one public method comes next, loadFeed(). This method accepts one argument: the URL of the feed to load, which will be supplied to it in the Flash movie by whatever menu system we elect to build. Somewhere in the timeline of the Flash movie, an instance of ProcessRss will be created and its one public method will be invoked. (Note the use of POST. This is the communication protocol used by the “improved” rssProxy.php script described on page 12.)

When loadFeed() is invoked, a LoadVars Object is created and given a property (“rss”). One of the methods of the LoadVars Object is sendAndLoad() which is extremely useful when engaging in a back-and-forth communication with a server script. A bit of data can be sent to the script and the return can be caught. The XML Object also has a sendAndLoad() method and, in a manner that is truly useful, the two can mix and match: the receiving Object doesn’t have to be a LoadVars Object! Notice that we target our XML Object (“_xml”) as the target for what comes back from the proxy script. So senderObj sends the URL of the feed we want to rssProxy.php, and _xml catches what the proxy sends back.

Assuming rssProxy.php has done its job, _xml responds to the event by broadcasting its onLoad() event, triggering the callback function which we assigned earlier to be processFeed(). Since this was covered in depth on page eight, I think that covers everything that might be new to you in the parsing Class. That wasn’t too terrible - was it?

That’s all there is to the parsing class. The rest of the code will reside on the movie timeline. Page ten discusses the preliminary set-up of the Flash .fla, and page eleven concludes with the timeline code that instantiates the Class, builds the interactive menu and adds styling refinements ot the components.

go to page: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13
divider ornament