XPathAPI – Actionscript 2.0
XPath API:
XML used to make our flash applications more dynamic.
The native XPathAPI class allows us to search the XML nodes and attributes within XML that €™s loaded in to flash.
Simple XML Structure:

Loading XML:
The basic actionscript code for loading XML in actionscript 2.0
var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function( isLoaded:Boolean )
{
if(isLoaded)
{
// here this refers to the xml object. Instead of this we can also use xml there.
trace( this );
//trace( xml );
}else
{
trace("XML loading error");
}
}
xml.load( "XML/store.xml" );
Output:
Entire xml content.
Explanation:
var xml:XML = new XML();
Declaring the xml object that holds the externally loaded XML data.
xml.ignoreWhite = true;
Ignoring the unwanted whitespaces in loaded XML.
xml.onLoad = function(){}
onLoad function triggered once the xml is loaded using xml.load method is called. onLoad function also checks whether the load is success or not.
Importing XPath API:
First step before using Xpath Api is to import the XPathAPI class. This will make us to use the methods of the XPathAPI in our code.
Import mx.xpath.XPathAPI;
Or
mx.xpath.XPathAPI.[methodName]
XPath API methods:
selectNodeList()
selectSingleNode()
XPathAPI.getEvalString()
XPathAPI.setNodeValue()
getEvalString:
A string that represents the code required to access the value specified by the path parameter from the parent node.
import mx.xpath.XPathAPI;
var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function( isLoaded:Boolean )
{
if(isLoaded)
{
//trace( this );
// or
//trace( xml )
var myEvalString_str:String = XPathAPI.getEvalString(this.firstChild,"store/item/name");
trace("getEvalString: " + myEvalString_str);
//output
//getEvalString: this.firstChild.childNodes.0.childNodes.0.firstChild.nodeValue
//we can use this to locate the value, for example
//add this.firstChild infront bcoz we give this as the first argument for getEvalString.
//replace childNodes.0 with childNodes[0]
trace(this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue)
//Outputs Pencil
}else
{
trace("XML loading error");
}
}
xml.load( "XML/store.xml" );
Explanation:
Outputs:
getEvalString: this.firstChild.childNodes.0.childNodes.0.firstChild.nodeValue
this method is useful to locate the value, for example
add this.firstChild infront bcoz we give this as the first argument for getEvalString.
replace childNodes.0 with childNodes[0], this forms this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue
now trace the value and see
trace(this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue)
selectNodeList:
Method returns an array of nodes matching the given XPathAPI statement.
import mx.xpath.XPathAPI;
var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function( isLoaded:Boolean )
{
if(isLoaded)
{
//trace( this );
// or
//trace( xml )
var nodeList_arr : Array = XPathAPI.selectNodeList(this.firstChild,"store/item/name");
trace("selectNodeList: " + nodeList_arr);
//output
//selectNodeList: <name>Pencil</name>,<name>Eraser</name>,<name>NoteBook</name>
//nodeList_arr contains collection of XMLNodes with nodeName as name
//to get the values from XMLNodes use looping statements
for(i=0;i<nodeList_arr.length;i++)trace("Value :"+nodeList_arr[i].firstChild.nodeValue);
}else
{
trace("XML loading error");
}
}
xml.load( "XML/store.xml" );
//output
//selectNodeList: <name>Pencil</name>,<name>Eraser</name>,<name>NoteBook</name>
//Value : Pencil
//Value : Eraser
//Value : NoteBook
Explanation:
store/item/name defines the path of node name (name)
we cal also use
*/*/name
This retrives the XMLNodes with nodeName €“name, selectNodeList returns array.
To get the nodeValues we need to use looping statements to loop through the array.
SelectSingleNode:
This XPathAPI method retives the XMLNode which matches the query we can also use AND, OR statements while using the query.
import mx.xpath.XPathAPI;
var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function( isLoaded:Boolean )
{
if(isLoaded)
{
//trace( this );
// or
//trace( xml )
var xmlNode:Array = XPathAPI.selectNodeList(this.firstChild,"store/item[@id='1']");
trace("selectSingleNode: " + xmlNode);
//output
//selectSingleNode: <item id="1"><name>Pencil</name><price>10</price><code>1120</code></item>
//Here the query retives the node which has the attribute id ='1'
}else
{
trace("XML loading error");
}
}
xml.load( "XML/store.xml" );
Download Macromedia XPathAPI documentation











































Leave your response!