Finding a specific node in an AS3 XML object

Continuing with my updating an Actionscript 2 project to Actionscript 3 I’m really enjoying the optimisation possibilities that AS3 provides.

For example, In my as2 code I had a recursive method for finding a node.

// recursive node search
private function findNode(n:XMLNode,name:String,attname:String,attdata:String):XMLNode{

var xNode:XMLNode;
if(n.hasChildNodes()){

for(var i = 0; i < n.childNodes.length; i++){

if(n.childNodes[i].nodeName == name){

if(n.childNodes[i].attributes[attname] == attdata){

xNode = n.childNodes[i];

}

}
if(xNode != undefined){

break;

}else if(n.childNodes[i].hasChildNodes()){

xNode = findNode(n.childNodes[i],name,attname,attdata);

}

}

}
return xNode;

}


In as3 this is now so much cleaner:

private function findNode(n:XMLNode,name:String,attname:String,attdata:String):XMLNode{

return n.descendants(name).(@[attname] == attdata);

}


This makes me happy.

This entry was posted in Code, flash and tagged , , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Peter
    Posted April 7, 2010 at 11:59 am | Permalink

    Hi, can you email me? I’m interested in hiring you to do some custom flash work with one of your pieces I found on activeden but I cant seem to find any way to contact you (completely could be missing it).

    Thanks!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

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>