gSkinner - Home

Code snippet: XMLNode.indexOn()

Posted on September 3, 2003 by Grant Skinner

I plan to blog useful code snippets regularly. Just little bits of ActionScript that help me in my day to day coding.

To start, I want to share a little snippet that is possible the most useful 14 lines of code I ever wrote. It’s a simple addition to the XMLNode prototype that lets you index and reference a node’s childnodes based on any attribute or the nodeName. It will be less useful with AS2.0, due to the xPath implementation, but I expect it will still be faster, and will do the job in many situations.

I’m not sure I would ever have had the patience to write gModeler if it were not for this little gem.

Read on for the code…


Here is the code, and comments. Hopefully it comes through ok – I’m new to this game. :)

/*

Code snippet by Grant Skinner [ http://www.gskinner.com ]

This is one of those ultra simple snippets that I can’t live without. All it does is take an xml node, and creates an index object for it.

The index object references the child nodes by the value of the attribute passed to the method, or by nodeName.

Usage:

myXMLNode.indexOn(attributeName);

myVariable = myXMLNode.index.attributeValue;

// where attributeName is the name of the attribute to base the index on

// or “nodeName” to index by the node names.

*/

XMLNode.prototype.indexOn = function(fAttribute) {

  this.index = {};

  var children = this.childNodes;

  if (fAttribute == “NODENAME”) {

    for (var i in children) {

      this.index[children[i].nodeName] = children[i];

    }

  } else {

    for (var i in children) {

      this.index[children[i].attributes[fAttribute]] = children[i];

    }

  }

  delete(this.index[null]);

}

Add this code near the beginning of a flash movie, and you can use the indexOn method on any xml node to create an index for the node based on the attribute name you passed (or the nodeName if you pass in “NODENAME”). Here is a simple example:

// create the xml object:

myXML = new XML(“<book><chapter name=’test1′/><chapter name=’test2′/><chapter name=’test3′/></book>”);

// get the root node (book):

myBook_xml = myXML.firstChild;

// create an index based on the “name” attribute

myBook_xml.indexOn(“name”);

// access the childNode with the name attribute equal to “test2″, and trace it’s value:

myChapterNode = myBook_xml.index["test2"];

trace(“test2 xml= ” + myChapterNode);

Follow @gskinner on Twitter for more news and views on interactive media.
6 Comments

grant,

interesting. one of the drawbacks of xml is actually being able to have a structure that is mapped. i like your approach. im going to test it further with some large structures. it looks nice though. how far have u stressed tested it?

Posted by: kenny bunch on Sep 3, 2003 2:11pm

Kenny,

It scales extremely well. The indexing process is very simple (and hence not processor intensive), and the processor requirement to access the index is virtually nil.

Besides it being used with 200kb+ gModeler files, I have also run speed tests with it on XML nodes having 2000+ children with no appreciable speed issues.

Cheers.

Posted by: Grant on Sep 3, 2003 3:10pm URL: http://gskinner.com/

Hey Grant,

Blogs looking good, but if yer gonna post code snippets you should check out Sean Voisen's MTCodeBeautifier. Just makes everything that much cleaner.

http://voisen.org/archives/projects/000239.php

Cya in a few hours at the Mx 04 Deally...

Posted by: Ryan on Sep 3, 2003 4:23pm URL: http://www.chewtinfoil.com

this reminds me of something: when will someone finally implement DOM2 for flash? do i have to do it? ;)

Posted by: Claus Wahlers on Sep 3, 2003 9:37pm URL: http://w3blog.com/news

Grant,

Is it faster in this case to do the for in loop or to do a while loop that decrements, since all for loops get converted to while loops in the player? Have you tested it the other way?

-Kenny

Posted by: Kenny Bunch on Sep 5, 2003 8:55am

ËͤâFlash¤ÇXML°·¤¨¤ë¥è¡ª¡ª

1ǯ¤Û¤ÉÁ°¡¢Flash¤Ç³°Éô¤Î¥Ç¡¼¥¿¤ò°·¤¦»þ¤ËXML¤ò¸¡Æ¤¤·¤¿¤Î¤À¤±¤É¡¢NodeName¤äattribute¤ÇÃͤò»²¾È¤Ç¤­¤Ê¤¤¤Î¤Ç¡¢¸½¾õMX¤Ç¤Ï¤¢¤¨¤ÆXML¤ò»È¤¦¤Û¤É¤ÎÍøÊØÀ­¤¬Ìµ¤¤¤È¸À¤¦·ëÏÀ¤Ë㤷¡¢ÉáÄ̤ËloadVariable¤ÇÆÉ¤ß¹þ¤à¤³¤È¤Ë¤·¤¿¤Î¤Ç¤·¤¿¡Ê´û¤ËXML¤òÅǤ¯µ¡¹½¤¬¤¢¤ë¾ì¹ç¤ÏÊ̡ˡ£ ¤...

Posted by: Dragonfly's blog on Sep 7, 2003 8:41am URL: http://WWW.salesaid.jp/mt/archives/000244.htm…

Leave a Reply

Your email is never published nor shared.




You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">