dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: getElementListByName() method for XML

  1. #1
    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;
    }

  2. #2
    Senior Moderator
    Join Date
    Apr 2000
    Location
    Sheffield, UK
    Posts
    3,881
    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:

    http://www.flashmove.com/board/showt...?threadid=2628

    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

  3. #3
    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!).

  4. #4
    Senior Moderator
    Join Date
    Apr 2000
    Location
    Sheffield, UK
    Posts
    3,881
    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:

    Code:
    XMLnode.prototype.getElementsbyName=getElementsbyName
    _root.myxml.firstChild.nextSibling.getElements()
    Or you can just make it a standard method of the xml object like for example the Selection.getFocus() method:

    Code:
    XML.getElementsbyName=getElementsbyName
    XML.getElementsbyName("news")
    What information would u like to see or hear?!

  5. #5
    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.

    Code:
    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center