Flash MX 2004 RSS Reader Pg.5

source: http://www.thegoldenmean.com

5 — Node Matching using Recursion

Node Matching

The Objective:

We want to write data to a TextArea component in our 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 just want to zero in on the <item> nodes, from them extract the text data of the <title>, <link> and <description> nodes and finally send all that to a TextArea component wrapped in formatting tags.

So wouldn’t it be nice to just be able to find the nodes you need?

Our problem is easily stated: we want to locate and isolate any occurrance of a particular node (in our case that would be any node named “item”) no matter how many of them there are and most importantly no matter where in the document’s structure they are located. This means we need a script that will examine the entire document. A script that will follow every twisted branch of the document’s tree, looking for every match to whatever we ask it to find.

Grant Skinner wrote an interesting XML prototype he called “indexOn()”. Similarly, Ted Patrick presented code he calls “XML Path Resolver”. Both of these efforts are fast and powerful but, unless I misunderstand them, neither is recursive and thus they are both more suited to XML documents that are not deeply nested.

On the previous page we saw how to write a script that would look for a particular node if we know the document structure. As we have seen, this is not something we can count on with various flavors of RSS. What if we don’t know (or can’t know) that? How can we drill down through the entire XML heirarchy looking for matches to the node we seek? We turn to a very powerful programming technique called “recursion”.

Recursion

Ouroboros represents the cyclical nature of things, eternal return, and other things perceived as cycles that begin anew as soon as they end. (wikipedia.org)A recursive function is one that actually calls itself from within the function. It is sort of like one of those tunnels of mirrors where you see yourself endlessly reflected. Of course, “endlessly” is something we want to avoid in programming or else we lock our computer up completely. A recursive script with some sensible limit can be very useful indeed, especially for the task at hand - exploring every node and every child node of an XML document.

Benjamin Mace wrote a concise and helpful article on using recursive functions which I recommend you read to get a better understanding of how it all works.

One solution to our problem is to write a function that looks at the child nodes and passes a new child node to itself until there aren't any more children to examine.

Pages Six and Seven demonstrate two approaches. The first approach pushes all the <item> nodes if finds into an array, and then in a second pass examines that subset for the data nodes. The version on the next page runs a for-in loop on every <item> node it encounters, building the final output string in one pass. Both versions are much faster and more adaptive than the approach discussed on the previous page.

One final note before we begin. Pages Six and Seven discuss the relevant bits of parsing code in some depth but don’t cover writing the entire class, nor do they discuss using the parsing class in a Flash movie. Included in the project files download are three complete, functional, extensively commented variations of an RSS reader. It is expected that you will read the tutorial and then study the ActionScript class and .fla files contained in the project folder. The code blocks on the next three pages will be presented as methods of a class, not stand-alone procedural functions.

With a little bit of recursive theory behind you, let’s move on to page 6, where we will build a parsing engine that stores data in arrays.

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

--top--