The Dark Art of the XPathAPI

I’ve personally been using the XPathApi in flash for a little while. I don’t remember how I came across it initially, but I do know that I use it in almost every project I have worked on lately. However it has been bought to my attention by a colleague that not everyone has fallen across this most handy of classes. Below is information and links to help you get the most out of the XPathApi class in flash.

I am sure everyone is aware of the XML Object in flash and the ability to load and manage structured XML data in a flash movie. One of the challenges is how to access all that lovely data in a quick and easy method.

On numerous occasions on site I have seen an almost infinite number of XML parsers that will take in an XML document or Object and through recursive functions or the like rip the various XML nodeValues and attributes into native flash Objects, Arrays and Variables.

These would then be used through out the movie as any other data set. There is of course nothing wrong with this system, and at times in the past I too have gone about development in this manner, apart from the fact that it is ultimately a waste of time.

In flash we are provided with the ability to access the data held in an XML Document and or Object natively, Everyone will likely be aware of the Node.firstChild.nodeValue access notation. This allows us to preserve the structure of an XML Document while still having access to the data it holds. The downside of course is that the Syntax can become long and unwieldy if you are processing a complex XML structure.

A second option for dealing with XML in Flash has been provided as of Flash MX 2004 Professional edition. With the DataBinding components in Flash MX 2004 Pro was provided the mx.xpath package of classes. Adobe have also included this package in Flash 8. In addition the classes within the xpath package are compatible with Flash 6 MX so they operate well on the PSP Flash player as well.

The XPath implementation provided is not a complete XPath Implementation. But it does provide a susinct method for accessing XML Nodes by name, and or attribute values in a simple syntax.

Below is a simple snippet to highlight use of the XPathApi in Flash.

/* example XML Structure used in this snippet.
< ?xml version="1.0" encoding="UTF-8"?>

//import the xpath api class
import mx.xpath.XPathAPI;
// create a new XML Object that will load the XML Data
var myXMLDocument:XML = new XML();
myXMLDocument.ignoreWhite = true;
// create an onLoad Event Handler for the XML Object
myXMLDocument.onLoad = function(bln_success:Boolean)
//the string xpath value
var xPath_str:String = “bittubesite/navbar/navitem”
// the selectNodeList method of XPathAPI returns an array of XML Node Objects
var navLabels_arr:Array = XPathAPI.selectNodeList(_projectInfo.firstChild, xPath_str);

// trace the values stored in the array generated by selectNodeList
trace(“navLabels_arr” + navLabels_arr);
/* outputs

trace(“XML Load Error!!”);
// load the xml document

Unfortunately there is a lack of official documentation to the mx.xpath package in the flash documentation, which is a little frustrating.

There is a more complete implementation of the W3C XPath specification that has been produced by xfactorstudio. This is available in both ActionScript 1 and ActionScript 2 versions.

There are a number of other articles that have been written that offer more detail of the capabilities of provided by the built in mx.xpath package I have linked to them at the end of this post along with some other xpath resources for those that want to find out more.

xpathStudio — Alternative and more complete XPath implementation
mx.xpath.XPathAPI vs xpathStudio
using XPathAPI by by peach pit
using XPathAPI by last actionscript hero
using XPathAPI by Jen deHaan
W3C XPath Specification

4 thoughts on “The Dark Art of the XPathAPI”

  1. I never really started using the MM classes, but I’ve been using XFactorStudios for some time and have found them to handle some really quite complex XPath (including the in-line function evaluation) without problem. I use a tool to help build and test XPaths, you can try it at:

    IE Only:

    You could download this at:

    But link seems to be down for the moment. I did find that a few things were inconsistent. In particular the XPathVisualiser (and XPath standard) allowed me to use a “.” in the following XPath:


    This would select all nodes with _texthere_ in the node name. But the XFactorStudio implementation worked when I ommited the “.” in the (). Other than that no problems so far, thoroughly recommend it!

  2. not really sure how using an object as a container for parsed XML is “ultimately a waste of time”?

  3. My intent was to point out that producing extra ‘lumps’ of code and burning extra CPU cycles just to parse XML from one form of Object (the XML Object it loads into) into an-other set of Objects (i.e. Objects and Arrays) is, in most cases, a waste of time.

    I was not questioning the storage of data in Objects at all, indeed I would be a mad psychotic developer if I had no Objects, and Object based structures available in Flash. My point was rather that moving it from one place to an-other is not required, and doing things that aren’t required are “ultimately a waste of time”.

  4. What code editor do you use for Word Press? I’m searching for a way to preserve my code tags. I like how yours also preserves the color.

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *