I mentioned in another thread that I wanted to extend the prototype for the XML class. As it turns out, what I wanted to do actually required extension of the XMLNode class prototype instead. Below I have the code for a new method that will recursively search an XMLNode tree (including children) and return an array of XMLNode objects that have a root object whose node name matches a string passed to the method. I wanted to know if anyone had any ideas/comments on this method or had ideas for how it might better fit a true XML DOM spec.
I figure that if it is written correctly, I could add it to a .as file for inclusion in projects where I need better XML support.... and with any luck (if I've written this right), I won't have to do much revision to code if MM decides that this sort of stuff should be included in Flash 6.
I'm anxious to hear comments -- Thanks!
Code:function getElementListByName (str) {
// PARAMS
//
// STR is a string that is to be matched against element names within
// the node (XMLNode) and its children (XMLNode).
//
// RETURN
//
// an array of XMLNode objects where the node names match STR.
//
var nodeArr = new Array();
var searchNode;
if ( !this.hasChildNodes() ) {
// no children to search
return nodeArr;
}
// descend and search children
searchNode = this.firstChild;
while (searchNode != null) {
if (searchNode.nodeType == 1 && searchNode.nodeName == str) {
// match!
nodeArr.push( searchNode );
}
if (searchNode.nodeType == 1 && searchNode.hasChildNodes()) {
// search through child nodes recursively
nodeArr = nodeArr.concat( searchNode.getElementListByName(str) );
}
// advance to next node
searchNode = searchNode.nextSibling;
}
// return the array to original call, or to the last recursive call
return nodeArr;
}