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);
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?
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.
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…
this reminds me of something: when will someone finally implement DOM2 for flash? do i have to do it? ๐
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
รรยครขFlashยครXMLยฐยทยคยจยครซยฅรจยกยชยกยช
1รยฏยครยครรยฐยกยขFlashยครยณยฐรรดยครยฅรยกยผยฅยฟยครฒยฐยทยคยฆยปรพยครXMLยครฒยธยกรยคยคยทยคยฟยครยครยคยฑยครยกยขNodeNameยครคattributeยครรรยครฒยปยฒยพรยครยคยญยครยคยคยครยครยกยขยธยฝยพรตMXยครยครยคยขยคยจยครXMLยครฒยปรยคยฆยครยครยครรรธรรรยญยคยฌรยตยคยคยครยธรยคยฆยทรซรรยครรยฃยคยทยกยขรรกรรยครloadVariableยครรรยครยนรพยคร ยคยณยครยครยคยทยคยฟยครยครยคยทยคยฟยกรยดรปยครXMLยครฒร รยคยฏยตยกยนยฝยคยฌยคยขยครซยพรฌยนรงยครรรยกรยกยฃ ยค…