Flash RSS Reader Pg.8
source: http://www.thegoldenmean.com
8 — Using XPath
Version Three: The Path of Least Resistance
Restating The Objective:
One last time: We want to present data using a TextArea component in a Flash Movie, formatted with HTML tags and structured like this:
<headline><a href="LINK">TITLE</a></headline><p>DESCRIPTION</p>
(where the words in all caps above are the actual content from an RSS document). There might be three or thirty entries. The script shouldn’t care. We simply want to target the <item> nodes and from them extract the text data contained in their <title>, <link> and <description> child nodes.
Introducing a Power Tool: XPath
What is XPath?
I stated earlier in this tutorial that XML is an absolutely fundamental data-exchange technology. XML is certainly not limited to the internet. If XML is so fundamental, you’d think someone whould have come up with a way to make it easier to use. You would have thought correctly. XPath is actually a language, and furthermore is a w3.org standard. For those interested, much MUCH more can be read at w3.org’s XPath specifications page
To summarize (quoting from www.w3.org/TR/xpath):
“XPath is a language for addressing parts of an XML document…
XPath gets its name from its use of a path notation as in URLs for navigating through the heirarchial structure of an XML document…
XPath is also designed so that it has a natural subset that can be used for matching (testing whether or not a node matches a pattern)…
XPath Primer
I am not going to attempt to go into any depth about the many things XPath is capable of. You are encouraged to learn at least the basics by reading a wonderful tutorial: the w3schools XPath tutorial
Great. Does it work with Flash?
Luckily for us, an industrious person named Neeld Tanksley undertook the task of writing ActionScript Classes to bring at least most of XPath’s functionality to Flash coders. He has both ActionScript 1.0 and 2.0 versions available at his site, xfactorstudio.com. I encourage you to visit, look at some of the other work he has available, and download the latest version of XPath for ActionScript 2.0.
Building the Parser using XPath
Even if you have downloaded my project files (which includes a copy of Tanksely’s classes), I would recommend that you check the xfactorstudio site - this is an on-going project for Neeld and he might have a new version that wasn’t available when I wrote this. In any event, be certain to keep the XPath classes in the same directory structure they come in. This is an ActionScrpt 2.0 “package” and it needs to be stored in nested directories like this:
com > xfactorstudio > xml > xpath
If you “unpack” the package and rearrange the contents it will all fall apart. Keep the package the way it comes!
Our first job is to make the XPath code available to Flash. The first line of the Class therefore includes all of Tanksely’s classes:
import com.xfactorstudio.xml.xpath.*;
(At the time of this writing Neeld had 13 individual class files in the set. Using the wildcard character "*" we can bring in every one of them in one statement. Although you might imagine this could add unnecessary bloat to the Flash movie, it turns out that Flash will only compile the classes it uses. Pretty sweet!)
I decided to go back to using arrays to store data as it is extracted, but in a more efficient way than on page six. The class declares three arrays, and then this beautiful little bit of code populates the arrays:
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 displayFeed(); }
I don’t know about you, but I was quite blown away by XPath. It does seem to execute somewhat slower than the hand-made versions on the previous pages but because XPath is so predictable and easy to work with I think it’s worth the small price in performance. Note the syntax “//item/…”. If you read the w3schools’ tutorial you will recognize that this means “item node, wherever it occurs”. Wasn’'t that easy?
The information is now stored in three arrays. To get it formatted to conform to the Objective stated at the top of this page, nothing beats a good old for loop. Presumably all three arrays have the same number of items, so pick one of them to use for the loop’s limiter. I chose “titles”. The following method loops through all three arrays, building the output string and sending it to the TextArea component as it goes instead of all at once at the end. Like so:
private function displayFeed():Void { 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]+"</a></headline><p>"+ descriptions[i]+"</p><br>"; } }
Moving on…
The last three pages have demonstrated three very different approaches to extracting the data we want out of an RSS document. It’s time to choose one and make a real project out of it. I have opted to use the XPath version so the next page of this tutorial will deal with writing the whole RSS Parsing Class. And finally, on page ten you will at last get to start up Flash!
Onward to Writing the Class…
--top--