==== XPath4AS2 ==== === Description === This AS2.0 [[http://xfactorstudio.com|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 [[http://xfactorstudio.com|XFactor Studio]] is available here [[http://xfactorstudio.com/downloads/XPath4AS2.zip|XPath4AS2.zip]] * An updated version of the library that compiles with the -strict switch of mtasc is available here [[http://www.ostranslations.com/xpath4as2/xpath4as2-strict.zip|xpath4as2-strict.zip]] * An updated version of the mtasc-strict-compliant library, including a fix for the translate command, is available here [[http://stoletheshow.com/fileadmin/xpath4as2-strict-translate.zip|xpath4as2-strict-translate.zip]] === Example === Here is a usage example with the following XML, Products.xml: Big Metal Pot 19.95 Plate 12.95 Spoon 4.95 Coffee Machine 234.20 3 ans Céréales 3.95 12.10.05 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 *
of the methods of the XPath4AS2 library. *

* You need to create the com.dynamicflash.utils.Delegate class file *
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 *
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)");