//richinternet.blog

Viewing By Entry / Main
Monday, June 14, 2004
XPath in Flash - there's always more than one way to go

Only few seem to know that a XPath engine is already included in Flash MX 2004 - ok, I have to admit that it not fully implements the whole XPath specification but it does a great job anyway (especially if you're tired of looping through XML structures and doing all that childNodes[n].firstChild.firstChild.nodeValue stuff).

Flash MX 2004 comes along with a slightly undocumented XPath API. Flash uses this API internally to resolve author-time data bindings. If you've ever done data binding visually in Flash then you'll probably know that there always is this "Use path expression" checkbox when the "Add Binding" dialog pops up - that's what makes me curious one day ;)

Important: the API is only availble in its compiled form and resides inside the DataBinding SWC file. This component has to be added to the library, otherwise the movie won't compile!

So - here's how to use the XPath API:

  • create a new fla with Flash MX 2004
  • open the "Classes" library (Window --> Other panels --> Common Libraries --> Classes)
  • drag & drop the DataBindingClasses component to the stage
  • delete the component instance from the stage (check your fla's library to see the component is still inside it)
  • make sure to add import mx.xpath.XPathAPI;

The XPath API is implemented as a static class. Two methods of this class are the most helpful:

  • XPathAPI.selectNodeList(node:XML, statement:String):Array
  • XPathAPI.selectSingleNode(node:XML, statement:String):XMLNode

The first method retruns an Array consisting of XMLNode objects, the second method returns one single XMLNode. The implementation is pretty straightforward and returns results very quickly even when searching on big XML trees. The returned elements can again be searched via XPath as they are XMLNodes (and therefore valid parameters to the search methods)

The only drawback is that the API does not support the 'greater than" and 'less than' operators - in other words: you cannot perform this statement: /catalog/books[pages > 420]

I compiled a quick example here for your testing pleasure.

Same topic, just another technique: Last Actionscript Hero shows in his blog how to use XPath in Flash MX 2004 by using the XPath library by xfactorstudio.

Dirk.

Comments

very nice. i had no idea that was in there.


Yeah - it's one of those hidden treasure thingies ;)

Dirk.


Thanks for the FLA. nice to just download and compile.


// import the classes necessary to do the xpath xml searching import mx.xpath.XPathAPI; // myXML = new XML(); myXML.ignoreWhite = true; myXML.onLoad = function(Success:Boolean) { // grabs all the <Table> nodes path = "/kms_response/KioskList/*"; tableNodes = XPathAPI.selectNodeList(this.firstChild, path); //trace("tableNodes = "+tableNodes); //trace("tableNode.length = "+tableNodes.length); for (var i = 0; i<tableNodes.length; i++) { tableNode = tableNodes[i]; idNode = XPathAPI.selectSingleNode(tableNode, "kioskid"); trace(""); trace("idNode = "+idNode); //trace("tableNode = "+tableNode); //XPathAPI.selectSingleNode(node:XML, statement:String):XMLNode } }; myXML.load("kioskList.xml"); ======================================= For whatever reason I am unable to get the value of the kioskID node no matter what I try.


Devin,

make sure you've got the DataBinding Classes inside your fla's library and also an instance of that symbol on the stage.

Dirk.


hi , wonderfull topic here.

but how would i assign a "querry" to a button? cuz now it all loads

fe. i only want to show ("Total <food> elements: " + arr.length);

on a buttonclick in a text field

i know it's something like txt.text = "Total <food> elements: " + arr.length);

but it doesn't do it well, i get no result in the text field but can anyone assist me?


Thank you -- Thank you -- Thank you...

Jim


Thanks a lot man, this is really usefull.

See ya, Juanca http://www.sistemas7g.com




Sun Mon Tue Wed Thu Fri Sat
   1234
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30   

About this blog
www.richinternet.de

AIR (9)
Apollo (3)
BlazeDS (8)
Breeze (1)
Central (5)
ColdFusion (54)
Flash (57)
Flash Media Server (6)
Flex (160)
Flex Trace Panel (6)
FXUG (3)
J2Flex (4)
MAX (31)
Mobile (1)
mxmlc (1)
Other topics (42)
Plugins (5)
Video (4)

Copy Fully Qualified Classname Plugin for Flash Builder 4 (CFQCN)

Recent Flex Technotes
Recent ColdFusion Technotes
Recent Flash Technotes

http://www.flexperten.de
http://www.flexforum.de
http://www.flex.org
http://www.bloginblack.de
http://www.cflex.net
flexcoders mailing-list
flexcoders archive
more links...

Aggregated by fullasagoog.com
Aggregated by MXNA

Short Mode | Full Mode

Herrlich & Ramuschkat