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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access