How to Use the XPathAPI in Flash
XML has bridged the gap between technologies and added a lot of power to Flash, allowing it to be more dynamic and flexible for building large applications. XML in Flash has been a possibility since Flash 5 with ActionScript 1, and significantly improved when it became a native object in Flash Player 6 with the introduction of ActionScript 2. But along with all of this power came certain limitations that don’t exist in more complete programming languages. One huge difference was that there had been no implementation of the XPath in Flash, but this situation finally changed with the release of Flash 8. The native XPathAPI object in Flash 8 provides the capabilities to do simple searches for node names and attribute values in an XML file.
This article explains how to use the XPathAPI in Flash 8 to make your XML parsing extremely easy. We’ll explore the available methods and the many different ways to search your XML files. Let’s get started with an understanding of the methods available through the XPathAPI.
Introduction to the XPathAPI Object
Perhaps the most interesting thing about the XPathAPI is that it has been available since Flash version, but it was undocumented and could only be accessed through the DataBindingClass by adding the component to your movie. This also happens to be the way that the help documentation in Flash 8 explains how to use the class, but you no longer have to add the DataBindingClass to your file; all you have to do is add an import statement for the XPathAPI as follows:
Or you could call specific methods with the full path to the object plus the method name, like so:
Because the XPath methods have been available since version 6 of Flash, the code is compatible with Flash Player 6 (184.108.40.206). This means that you can create backwardly compatible projects and still make use of the XPathAPI methods, a small set that packs quite a bit of power:
- XPathAPI.getEvalString(node, path);
- XPathAPI.selectNodeList(node, path);
- XPathAPI.selectSingleNode(node, path);
- XPathAPI.setNodeValue(node, path, nodeValue);
The getEvalString method takes two parameters, a node and a path, and returns code as a string that would be required to target this node using standard XML parsing. It’s useful when you’re uncertain of the depth of a node and you want to see where it is in relationship to other nodes in an XML file.
The selectNodeList and selectSingleNode methods take the same two parameters, a node and a path. The path is used to search from the node location that was passed as the first parameter. For selectNodeList, it will return all the matching nodes as an array, whereas selectSingleNode returns the first matching node on its own. These methods are very powerful and can be manipulated to search for many different elements in an XML file, as we’ll see in the next section.
The setNodeValue method takes the same two parameters, a node and a path, with the addition of a nodeValue parameter that replaces the value of the node once it’s found. As you can imagine, these methods make XML parsing extremely easy and can be used to create more advanced searches, which we’ll learn about shortly.