//////////////////////// XMLNodeEasyAccess 1.01 //////////////////////// // Tatsuo Kato Nov. 2002 //////////////////////////////////////////////////////////////////////// /* This achieves easy access to XML nodes without converting your xml object to a plain object, using XMLNode.prototype.tagID and XMLNode.prototype.__resolve. tagID : Method that allows to add an 'id' attribute whose value is nodeName of the node that it belongs to, to every node in your XML object. If there are multiple nodes with the same nodeName under the same parentNode, number suffixes get added to the values of the IDs like : item_0, item_1, item_2, etc. XMLnode.prototype.__resolve : A handler that makes it possible to access nodes via pure dot-syntax using the id attributes. Note on id attribute : An id attribute becomes a shortcut to the node that it belongs to. */ XMLNode.prototype.tagID = function() { if (this.hasChildNodes()) { var nodes = this.childNodes; var checker = new Object(); var len = nodes.length; for (var i = 0; i < len; i++) { var node = nodes[i]; var name = node.nodeName; if (checker[name] == undefined) { node.attributes.id = name; checker[name] = {index:0, origin:node}; } else { if (checker[name].index == 0) { checker[name].origin.attributes.id += "_0"; } node.attributes.id = name + "_" + (++checker[name].index); } node.tagID(); } } }; XMLNode.prototype.__resolve = function(sID) { return new XML(this)[sID]; }; /* Usage example //xml doc example hello hi bye seeya test ciao cava nihao //After loaded and parsed, it'll be equivalent to: myXML = new XML("hellohibyeseeyatestciaocavanihao"); //Now let's add an id attribute to every node myXML.tagID(); trace(myXML); //hellohibyeseeyatestciaocavanihao //For readability, myXML is now: hello hi bye seeya test ciao cava nihao //Then you can access data like the following. trace(myXML.a_0.b_0.firstChild); //hello trace(myXML.a_0.b_1.firstChild); //hi trace(myXML.a_0.c_0.firstChild); //bye trace(myXML["a_0"].c_1.firstChild); //seeya trace(myXML.a_1.firstChild); //null trace(myXML.a_2.firstChild); //test trace(myXML.a_2.b.firstChild); //ciao trace(myXML.aa.b.firstChild); //cava trace(myXML.aa.d.firstChild); //nihao trace(myXML.d.firstChild); //nihao //Note: Nodes with unique nodeNames can be directly accessed from the //top level, i.e. myXML in this case, as seen in the last line as well, //as they should be. */ ////////////////////////////////////////////////////////////////////////