Click to See Complete Forum and Search --> : getElementListByName() method for XML

05-01-2001, 02:21 PM
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!

function getElementListByName (str) {
// STR is a string that is to be matched against element names within
// the node (XMLNode) and its children (XMLNode).
// 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;

05-01-2001, 02:29 PM
Me and a few other guy's had a talk about this a while back in the xml forum on flashmove, we managed to get a fully optimised, fast getElementsbyName function:


We created a way of avoiding the cpu hogging when parsing large xml documents!

Check the thread, it may help you out a bit!

Good work btw:)

05-01-2001, 02:37 PM
Awesome -- I'll check that out.

Are there other similar methods that you wrote as well? Or just the one you mentioned. (I'll check the Flashmove thread -- too many flash boards to follow and search.)

How well do you know the DOM? I'm looking for someone who might be able to tell me that I'm doing something completely wrong (or right!).

05-01-2001, 02:47 PM
There are two ways you could implement this into the xml object in flash, the first would be to make it a method of all xml nodes-basically it would be used as a function that would return an array of all xml nodes with the same nodeName:

XMLnode.prototype.getElementsbyName=getElementsbyN ame

Or you can just make it a standard method of the xml object like for example the Selection.getFocus() method:


What information would u like to see or hear?!

05-01-2001, 03:05 PM
Originally posted by FlashGuru
There are two ways you could implement this into the xml object in flash...

What I found was that I had to build it into XMLNode, as you suggest in the first example. The XML class, inheriting XMLNode, automatically has access to it from that point on.

XMLNode.prototype.getElementListByName = getElementListByName;

My original problems with this method stemmed from the fact that if it was only built into the XML class prototype, the method could not recursivly search child nodes since the child nodes were of the type XMLNode and not just XML.

I wanted to find out if there were classes for Element, Node, CharacterData, Text, and others that you *should* find in the DOM I guess. Technically, I think that only a couple of these should have access to the method I've built -- maybe Element and XML. A text node would have no use for searching for child elements, so it shouldn't really have access to this method ... but I haven't found any other classes besides XMLNode and XML.