Recursive Design


The flaw here is obvious. The code works all right, but it is severely limited. It is hideously ugly code ”far too redundant, too specific. There is too much code and ironically it does too little: we will only see the generations we have explicitly coded. This version goes down three generations from the root. Some XML will look fine. Some will show only a truncated picture of the data.

We can clean up the code by factoring all the display code into a function call. Note that we need to hook this function to the base XML object. This causes it to address the XML object's namespace. It shares this namespace with the onLoad function that will call it.

ActionScript
 xml.shownode = function ( x ) {   trace(    "type      "+ nodeTypeName[ x.nodeType]);   trace(    "name      "+ x.nodeName);   trace(    "value     "+ x.nodeValue);   for (name in x.attributes)       trace( "attribute " +name+":  "+ x.attributes[name]); } 

Now we can replace the large, ugly blocks of code with a function that will provide the output we want. We must be careful that the nodeTypeName array, which we refer to in the preceding code, is created in this same namespace as the function.

ActionScript
 xml.onLoad = function ( success ) {  nodeTypeName = new Array(          "",          "ELEMENT",          "ATTRIBUTE",          "TEXT",          "SECTION",          "ENTITY_REFERENCE",          "ENTITY",          "PROCESSING_INSTRUCTION",          "COMMENT",          "DOCUMENT",          "DOCUMENT_TYPE",          "DOCUMENT_FRAGMENT",          "NOTATION"            ); 

This array provides a mapping of the nodeTypes ( numbers defined in the XML DOM spec) to human-readable names . But it is a bit quixotic. Despite this heroic list of 12 node types, only two (Element (1) and Text (3) ) are supported as of this writing by Flash. (Text is the same as CDATA, Character Data.)

ActionScript
 trace( this.toString());   this.shownode( this ); for( var i=0; i< this.childNodes.length; i++){   this.shownode( this.childNodes[i]);   for( var ii=0; ii< ll=this.childNodes[i].childNodes.length; ii++){    this.shownode( this.childNodes[i].childNodes[ii]);    for( var iii=0; iii<this.childNodes[i].childNodes[ii].childNodes.length;                                                                     iii++){       this.shownode(this.childNodes[i].childNodes[ii].childNodes[iii]);       }     }   } } 

This code is a rewritten copy of the previous code in the chapter. Some of the code was not repeated, such as the function declaration. Nevertheless, it is obvious that the code is still horribly ugly. The explicit names of the multigenerational arrays are clumsy intellectually as well as typographically. Though they look neater, they are still arbitrarily limited. We need to step up to real recursion.



Flash and XML[c] A Developer[ap]s Guide
Flash and XML[c] A Developer[ap]s Guide
ISBN: 201729202
EAN: N/A
Year: 2005
Pages: 160

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net