Tom Klepl wrote:
Thanks for the reply.. unfortunately this only returns matching nodes below the parent node specified. What I need is to crawl the XML tree and find ALL matching nodes, regardless of where they are in the tree.

Unfortunately, Flash has never had a good means to traverse an XML tree. Macromedia's XPath API is a decent improvement to the out-of-the-box functionality, but if you need to do deep searches into your XML tree of the sort like the W3C DOM's getElementsByName and getElementById calls, you'll have to do it yourself.

I have previously written an open-source GPL implementation of the standards based W3C DOM Level 3 Core Specification for ActionScript 2 and wound up writing the traversal algorithms myself. Unless you really need a lot of the W3C DOM API though, I'd suggest sticking with the native XML implementation as it's about an order-of-magnitude faster than mine here:

  http://www.sourceforge.net/projects/ugo/

I made a quick and dirty rewrite of the pre-order traversal algorithm suitable for use with the native ActionScript 2.0 XMLNode object (as seems to be your case) below at the end of this message.

Should you need to traverse your XML tree in some other order, you can fairly easily change the while loop structure to accomodate this. If you need help doing this, you can probably compare against my implementation of the traverse method in the DOM3 Node class in the above project.

Hope this helps,

Jim Cheng
effectiveUI



<code>

/**
* Steps through an XML tree, calling the function on each node.
*
* @param  The function to call on each node.
* @param  The root node of the XML tree to traverse.
*/
function traverse(f:Function, rootNode:XMLNode):Void {
  var nodeList:Array = [rootNode], nodeList2:Array;
  var n:XMLNode, i:Number;
  // Pre-order traversal
  while(n = XMLNode(nodeList.shift())) {
    f(n, rootNode);
    if (n.childNodes.length) {
      nodeList2 = [];
      for (i = 0; i < n.childNodes.length; i++) {
        nodeList2.push(n.childNodes[i]);
      }
      nodeList = nodeList2.concat(nodeList);
    }
  }
}

// Now look for nodes named "nameOfNode"
var foundNodes:Array = [];
function isMatchingNode(n:XMLNode, root:XMLNode):Void {
  if (n.localName == "nameOfNode") {
    foundNodes.push(n);
  }
}
traverse(isMatchingNode, myXMLTree);

</code>

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to