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
// 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);
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