Xpath for ActionScript
I was implementing xpath for actionscript and I came across a couple of options. Flash MX 2004 has an undocumented built-in xpath class. You can access this class by dragging the DataBindingClass to the stage (windows – other panels – classes) and then importing the mx.xpath.XPathAPI. It works fine as I demonstrate in the following example.
var rssfeed_xml = new XML("<xml><level id='1' price='100'>first Level</level><level id='2' price='200'>second Level</level><level id='3' price='300'>third Level</level></xml>");
rssfeed_xml.ignoreWhite = true;
var titlePath:String = "/xml/level[@id=1]";
title_array = XPathAPI.selectNodeList(rssfeed_xml.firstChild, titlePath);
for (var i = 0; i<title_array.length; i++) {
trace(title_array[i].attributes.id);
}
However, this API does not support greater than or less than comparissions.
So I downloaded the Xpath open source API from Factor Studio. Just down the file and unzip it into your F:\Documents and Settings\[user]\Local Settings\Application Data\Macromedia\Flash MX 2004\en\Configuration\Classes\ folder.
Then you can import it and use it at will. It supports greater and smaller than comparisons, plus it supports union or queries using the pipe character.
var rssfeed_xml = new XML("<xml><level id='1' price='100'>first Level</level><level id='2' price='200'>second Level</level><level id='3' price='300'>third Level</level></xml>");
var levels = XPath.selectNodes(rssfeed_xml,"/xml/level[@price>150]");
for (var i = 0; i<levels.length; i++) {
trace(levels[i].attributes.id);
}
var rssfeed_xml = new XML("<xml><level id='1' price='100'>first Level</level><level id='2' price='200'>second Level</level><level id='3' price='300'>third Level</level></xml>");
var levels = XPath.selectNodes(rssfeed_xml,"/xml/level[@id=1] | /xml/level[@id=2]");
for (var i = 0; i<levels.length; i++) {
trace(levels[i].attributes.id);
}
updated 12/4/2006: link updated for Factor Studio
http://www.robgonda.com/blog/trackback.cfm?FEF3AD8C-2B3E-FB41-50446979B790E0DE
There are no comments for this entry.
[Add Comment]