XPath - A very cool way to do XML data in Flash
If you read my last blog, you'll see that I'm interested in using a FileMaker Pro database to populate Flash content. I'm relatively new to XML and using it in Flash, so I had a bit of a problem at first understanding how to get what I needed out of the XML file to display on screen. It seems like an endless series of "firstChild.firstChild.firstChild" to find the data you actually want. I finally figured out how to get to the right data by doing a trace in Flash and it told me when I got it right.
I then discovered a better way - XPath. According to the W3C, XPath is a language for addressing parts of an XML document. Although the standard is not completely supported in Flash, the parts that matter to me and probably most other Flash developers are. XPath allows you to take a look at your XML file and figure out what path you want it to use to easily get to the info you need.
Before you do anything in Flash, however, you need to go to the Window menu in Flash, and select Common Libraries. From there, select Classes, then drag the DataBindingClasses icon to your Library in your Flash doc. Once you do that, you can go to the first frame of your movie and set the following code in your Actionscript window (I've commented it out line by line):
// the following lines call the XPath class and create a new XML object in Flash
import mx.xpath.XPathAPI;
var my_xml:XML = new XML();
// tell it to ignore any white space it sees in the XML file
my_xml.ignoreWhite = true;
// Here is the function that will run when the Flash movie loads
my_xml.onLoad = function(success:Boolean) {
// Throw a couple trace messages to tell me that the XML file has loaded
trace("onload...");
if (success) {
trace("success...");
// Looking at my XML file, I decided that this was the path to the data I needed
path = "/FMPXMLRESULT/RESULTSET/ROW/COL/DATA";
// Make a array (list) of all the data
arNodes = mx.xpath.XPathAPI.selectNodeList(this.firstChild, path);
// Display the first and 9th pieces of data that you find in the text fields on screen
fname = arNodes[0].firstChild;
lname = arNodes[8].firstChild;
// Now I've made a new path to a higher level of data (just people's names)
path2 = "/FMPXMLRESULT/RESULTSET/ROW";
// And again I'll make an array of those names
namNodes = mx.xpath.XPathAPI.selectNodeList(this.firstChild, path2);
// Now just display the first one in the Output window in Flash
trace(namNodes[0].firstChild.firstChild.firstChild);
} else {
// In case of an error loading the file, trace the following
trace("error loading XML");
}
};
// This is actually the first thing that happens on the page - the XML file is loaded
// "fmp.xml" is the name of my XML file that I exported from Flash
my_xml.load("fmp.xml");
If you've ever addressed information from an XML file in Flash, you'll see that:
arNodes = mx.xpath.XPathAPI.selectNodeList(this.firstChild, path);
sure beats sure beats the snot out of trying to figure out the path manually. That usually looks like this:
name = this.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.firstChild.nextSibling.nextSibling.nextSibling.firstChild.firstChild;
And also note the this long line is only giving me 1 piece of data, while the XPath code gives me a whole array of similar information. This is a no-brainer for me. XPath is definitely the way to do XML in Flash!

Pat, this is very cool. You might want to talk with Chris Millet about some of this ... he did some Flash/XML stuff a few years ago while at IST. I know it was with a much older version of Flash so it may be very different. Looking forward to seeing some stuff come from this.