XPath4AS2
Description
This AS2.0 XPath Library allows ActionScript developers to use XPath within their Flash projects. You can easily retrieve XML nodes, Strings or Number with the simple XPath syntax.
Versions
- The original version from XFactor Studio is available here XPath4AS2.zip
- An updated version of the library that compiles with the -strict switch of mtasc is available here xpath4as2-strict.zip
- An updated version of the mtasc-strict-compliant library, including a fix for the translate command, is available here xpath4as2-strict-translate.zip
Example
Here is a usage example with the following XML, Products.xml:
<?xml version="1.0" encoding="utf-8"?> <products> <product SKU="7123734"> <name>Big Metal Pot</name> <price>19.95</price> </product> <product SKU="752585"> <name>Plate</name> <price>12.95</price> </product> <product SKU="4182476"> <name>Spoon</name> <price>4.95</price> </product> <product SKU="9909813"> <name>Coffee Machine</name> <price>234.20</price> <garantie>3 ans</garantie> </product> <produit SKU="1309384"> <nom>Céréales</nom> <prix>3.95</prix> <validité>12.10.05</validité> </produit> </products>
The following AS2 class easily retrieves all the data and puts it in Arrays:
import com.dynamicflash.utils.Delegate; import com.xfactorstudio.xml.xpath.XPath; /** * An example of loading and parsing data from an XML file using XPath * * @description The goal of this example is to show how to use a couple * <br />of the methods of the XPath4AS2 library. * <br /><br /> * You need to create the com.dynamicflash.utils.Delegate class file * <br />available here: http://dynamicflash.com/classes/Delegate.as * * @version 1.1 (Last modified: 2005/08/30) */ class ch.interfasys.test.XPath4AS2Tester { private static var xmlLoader:XPath4AS2Tester; private var _stage:MovieClip; /* The stage. Will be _root in this example */ private var _products:Array; /* The names holder */ private var _prices:Array; /* The prices holder */ private var _sku:Array; /* The SKU holder */ private var _xmlURL:String; /* The source of the XML feed */ private var _myXML:XML; /* The loader */ /** * Constructor */ private function XPath4AS2Tester(theStage:MovieClip) { this._stage = theStage; this._products = new Array(); this._prices = new Array(); this._sku = new Array(); this._myXML = new XML(); this._xmlURL = "Products.xml"; // Assigns this load method to the XML.onLoad method this._myXML.onLoad = Delegate.create(this, this.load); this.createDisplay(); this._myXML.load(this._xmlURL); } /** * Creates the textfield that will display the results */ private function createDisplay():Void { this._stage.createTextField("_info_tf", 1, 0, 0, 600, 600); this._stage._info_tf.selectable = false; this._stage._info_tf.text = "Loading XML"; } /** * Deals with the answer from the XML Loader * * @param success result of our attempt to load an XML */ private function load(success:Boolean):Void { // Checks if we received something and if it's a valid XML file if (success && this._myXML.xmlDecl != undefined) { this._stage._info_tf.text = "XML OK"; this.parser(); } else { this._stage._info_tf.text = "XML Problem "+success+", "+this._myXML.xmlDecl; } } /** * Parses the data loaded from the XML */ private function parser():Void { // All products this._products = XPath.selectNodesAsString(this._myXML, "//name/text() | //nom/text()"); // All the SKUs this._sku = XPath.selectNodes(this._myXML, "//product/@SKU | //produit/@SKU"); // All the prices this._prices = XPath.selectNodesAsNumber(this._myXML, "//price/ | //prix/"); this.display(); } /** * Displays the loaded data on Stage */ private function display():Void { this._stage._info_tf.text += "\nResults\n------"; for (var i:Number = 0; i < this._products.length; i++) { this._stage._info_tf.text += "\nName: "+this._products[i]+" (SKU:"+this._sku[i]+"), Price: "+this._prices[i]; } /** //Alternatives loop if you want to get one type of element for (var i:Number = 0; i < this._prices.length; i++) { this._stage._info_tf.text += "\n"+this._prices[i]; } for (var i:Number = 0; i < this._sku.length; i++) { this._stage._info_tf.text += "\n"+this._sku[i].nodeValue; } */ this._stage._info_tf.text += "\n------\nEOF"; } /** * Main function that initializes the stage for the application and * <br />loads an instance of the Xpath Testing application. * * @param stage the main stage of the application */ public static function main():Void { // Loads the application xmlLoader = new XPath4AS2Tester(_root); } }
It’s advised to type the full XPath when dealing with large data or the parsing may be very slow.
Some Paths example
Those paths can replace the existing ones in the parser() method.
// All english products this._products = XPath.selectNodesAsString(this._myXML, "//name/text()"); // All the SKUs for english products this._sku = XPath.selectNodes(this._myXML, "//product/@SKU"); // All the prices for english products this._prices = XPath.selectNodesAsNumber(this._myXML, "//price/number()"); // All english products which have a price higher than 5 this._products = XPath.selectNodesAsString(this._myXML, "//product[./price > 5]/name/text()"); // All english products starting from No3 this._products = XPath.selectNodes(this._myXML, "//product[position()>=3]"); // The last product this._products = XPath.selectNodesAsString(this._myXML, "//product[last()]/name/text()"); // Select the nodes that have three children this._products = XPath.selectNodesAsString(this._myXML, "//*[count(*)=3]"); // Some test showing the children this._products = XPath.selectNodes(this._myXML, "//product/descendant::*"); // Shows all the french products that have a warranty this._products = XPath.selectNodes(this._myXML, "//garantie/parent::*"); // Select the Nodes named prix this._products = XPath.selectNodesAsString(this._myXML, "//*[name() = \"prix\"]"); // Select the Nodes that start with a p this._products = XPath.selectNodesAsString(this._myXML, "//*[starts-with(name(), \"p\")]"); // Select the Nodes that contain the letters pro this._products = XPath.selectNodesAsString(this._myXML, "//*[contains(name(), \"pro\")]"); // Select the Nodes that contain the letters pro and is case-insensitive this._prices = XPath.selectNodes(this._myXML, "//*[contains(translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'pro')]"); //Creates an XMLNode with the ceiling of the prices this._prices = XPath.selectNodes(this._myXML, "ceiling(//price)");
xpath4as2.txt · Last modified: 2007/11/22 02:59 by nicolasschudel