-
·»¤«·
xmlobj.firstChild.childNodes[3] vs xmlobj.chapter.page
[EDIT] I got it to work. See following posts, as this one contains non-working AS [/EDIT]
I'm trying to make it so taht I can use tag names rather than all that 'firstChild' & 'childNodes' junk. Here's what i have:
code:
function tagNotation(xmltarget){
tagChildren(xmltarget);
//recusion function
function tagChildren(node){
for(var i=0 ; i<node.childNodes.length ; i++){//loop through current recursion node's children
node.childNodes[i].addProperty(node.childNodes[i].nodeName, function(){ return node.childNodes[i] }, null);
if(node.childNodes[i].hasChildNodes){//if current recursion node's child has children
tagChildren(node.childNodes[i])//recurse
}
//trace("_RETURN_");
}
}
It loops through all tags nicely. However, see where I'm trying to add a property? It doesn't seem to work. Anybody want to help me out with this? The thought was to simply add a property to every node which references it's children by name.
Last edited by >flashl!ght<; 12-29-2004 at 02:34 PM.
>flashl!ght<
All the normal names were taken.
Ron Paul was right.
-
·»¤«·
For those interested, I have manage a function hack to do it. Seems to work fine, along side any of the original referencing styles (firstChild, childNodes, ect.):code: //CHILDNODE
XMLnode.prototype.childNode = function(str,returnType){
//prototype that returns childNodes of current node
//with the specified tag name
//return either a single reference to a node if theres only one match
//or an array of all reference that match the specified tag name
//PARAMS:
//str: the name of the tag to find
//returnType(optional): "array" or "reference", explicitly sets the return value to be one or the other
ar=[];//reset return array
delete rtrn;//reset return reference
str=String(str);//maintain param order
returnType=returnType.toLowerCase();//remove case sensitivity
for(i=0 ; i<this.childNodes.length ; i++){//loop through all chilren (no recursion)
ni = this.childNodes[i];//reference to current child loop
if(ni.nodeName==str){//if match
if(rtrn!=undefined || returnType=="array"){//if match already found
ar.push(ni);//there are multiple matches, so add to return array
}else{//if no matches found yet
rtrn = ar[0] = ni;//set single return reference, and 1 array element just in case
}
}
}
if(ar.length>1 || returnType=="array"){//if more than one match found
return ar;//return array
}else{//if only one match found
return rtrn;//return single reference
}
}
Sample usages:
trace(_root.contentXML.firstChild.childNode("guns" ).childNode("mods").childNode("mod")[0].attributes.class);
trace(_root.contentXML.firstChild.childNode("jets" ).childNode("jet")[3].attributes.name);
engine = _root.contentXML.firstChild.childNode("cars").chil dNode("engines").childNode("engine");
trace(engine);
trace(engine[2]+"\n");
trace(_root.contentXML.firstChild.childNode("guns" ).childNode("mods","array")[0]);
>flashl!ght<
All the normal names were taken.
Ron Paul was right.
-
·»¤«·
just case there *is* someone interested, I have made my first step in solving it(ie it appears to work, but you never know):
code: //the usual XML load
contentXML = new XML();
contentXML.ignoreWhite=true;
contentXML.load("data.xml");
contentXML.onLoad = function(){
tagNotation(this);
}
//the function that sets everything to tag notation type referencing
//it recurses thru all nodes, assigning either
//a single reference, or an array of references
//to each node of all its children.
function tagNotation(xmltarget){
tagChildren(xmltarget);
//recusion function
function tagChildren(node){
for(var i=0 ; i<node.childNodes.length ; i++){//loop through current recursion node's children
child = node.childNodes[i];
if(node[child.nodeName]==undefined){//if no tag has been found with this name yet
node[child.nodeName] = child ;//set a simple reference
}else{//if a tag by this name ahs already been found - it returns an array
if(node[child.nodeName].length==undefined){//if no array has been generated yet
temp = node[child.nodeName];//save the one found so far
node[child.nodeName]=new Array();//reset the reference as an array
node[child.nodeName][0] = temp;//set the saved first match so far
}
node[child.nodeName].push(child);//add this node to the array
}
//child.addProperty(child.nodeName, function(){ return child }, null);
if(child.hasChildNodes){//if current recursion node's child has children
tagChildren(child)//recurse
}
}
}
}
//see next post for sampl usage
>flashl!ght<
All the normal names were taken.
Ron Paul was right.
-
·»¤«·
sample XML
PHP Code:
<xml>
<go>
<delta>
<blackhawk>
<wildfire>
<tango>
<varient>
<over gocode="Irene">Over and Out</over>
<over gocode="Bravo">Go!</over>
<over gocode="Down">TangoDown</over>
</varient>
</tango>
</wildfire>
</blackhawk>
</delta>
</go>
</xml>
sample referencing with above 'tagNotation' function:
code: _root.contentXML.firstChild.go.delta.blackhawk.wil dfire.tango.varient.over[0];
output: "<over gocode="Irene">Over and Out</over>"
_root.contentXML.firstChild.go.delta.blackhawk.wil dfire.tango.varient.over[1].attributes.gocode;
output: "Bravo "
_root.contentXML.firstChild.go.delta.blackhawk.wil dfire.tango.varient.over;
output: "<over gocode="Irene">Over and Out</over>,<over gocode="Bravo">Go!</over>,<over gocode="Down">TangoDown</over>"
Last edited by >flashl!ght<; 12-29-2004 at 02:31 PM.
>flashl!ght<
All the normal names were taken.
Ron Paul was right.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|