Recipe 19.15: Searching XML

Problem

You want to search an XML object for nodes based on
keywords and other criteria such as node hierarchy.

Solution

Use the third-party XPath class from XFactorStudio.com.

Discussion

Thus far in this chapter you've read recipes on how to work with XML objects using the DOM, or Document Object Model. This
means that if you want to locate a particular node in the XML tree, you need to
know the relationship of that node to the whole (i.e., first child, next
sibling, etc.). However, when you want a more flexible way of looking for nodes,
the DOM can become tedious.

XPath is a language that allows you a much more intuitive and flexible way to
find nodes within an XML object. XPath is a W3C standard
(see http://www.w3c.org/TR/xpath) that
is supported on many platforms, but it is not natively supported in Flash.
However, Neeld Tanksley of XFactorStudio.com has created an ActionScript XPath class that you can download from http://www.xfactorstudio.com/projects/XPath/index.php. You should download the .zip file and extract all the .as files into your Flash Include directory (make sure they are extracted into the Include directory, and not into subdirectories).

Once you have downloaded and installed the custom XPath class, you can include it in your Flash movies and begin using XPath to work with XML, as follows:

#include "XPath.as"

XPath uses path expressions to denote the node or nodes you want to find. For
example, if the root node in your XML object is named books, then you can find that root node using:

/books

If books contains child nodes named book, then you can return all the book nodes using:

/books/book

If you don't know or care about the full path from the root node to the node
or nodes for which you are searching, you can use a double slash to indicate
that you want to locate all matching nodes at any level in the XML tree. For
example, the following returns all author nodes
regardless of their hierarchy:

//author

An asterisk (*) is a wildcard. For example, the following matches all author nodes that are children of any nodes that are, in turn, children of the books node:

/books/*/author

You can also use square brackets ([]) to indicate criteria that the nodes
must match. For example, you can match all book nodes
that contain author nodes with the following:

//book[author]

Notice that the preceding is different from //book/author in that the former returns book nodes and the latter returns author nodes.

You can also use expressions with equality operators such as the following,
which returns all book nodes containing a child title node with a text value of "ActionScript Cookbook":

//book[title='ActionScript Cookbook']

The @ sign can be used to signify an attribute. The following example matches
all author nodes containing a name attribute:

//author[@name]

There are also many other built-in functions, operators, and keywords in
XPath that you can read about in the documentation.

The XPath class has one method that we are interested
in for this recipe. The XPath.selectNodes( ) method is a
static method, which means you invoke it from the class itself, not from an
instance of XPath. The method takes two parameters - the
XMLNode object to search and the XPath expression to
use - and returns an array of matching nodes:

matches = XPath.selectNodes(my_xml, "/books/book");

Now let's take a look at an example of XPath in use. For this example you
should make sure that you have installed all the .as
files for the XPath class.

Create an XML document using a simple text editor such as WordPad. Add the
following XML code to the document, and then save it as books.xml:

Open a new Flash document. Copy the PushButton component symbol into the
Library by dragging an instance from the Components panel onto the Stage and
then deleting the instance. The symbol remains in the Library. Then add the
following code to the first frame of the main timeline:

Joey Lott
is a founding partner of The Morphic Group. He has written many books on Flex
and Flash-related technologies, including Programming Flex 3, ActionScript 3 Cookbook, Adobe AIR in Action, and Advanced ActionScript 3 with Design
Patterns.