Pup 100
January 7th, 2005, 06:10 AM
Hi,
I have been working through some examples in a XML course book & have become totally stuck on one piece of code.
Although I understand 98% of what the code is trying to achieve, which, to surmise is to portray an XML document stripped of whitespace & properly spaced, I can not get my head around one particular section.
Set out below is the relevant portion of a larger segment of code (See attachment for full code) The portion in red is where I come unstuck.( The numbers set out in blue are not parts of the code but used to indicate parts of relevance in relation to the post.)
My understanding is the code is recursive so calls itself from within the main code digging deeper into the XML txt file, so if we take things from the function being called on the root element – At point 1. we know that the root element is nodeType 1 so it spaces it out, adds the name & attributes, no problem.
We know that the firstChild node is type 1 so the scripted adds a break and then calls the function recursively on all the childNodes ChildNodes, childNodes etc. etc.
Now if the firstChild only has one set of children, the function would be called on this child, which as the next node in this child would be text, at point 2, we would drop down to the “text section” (point 3.) & populate the string with the nodeValue.
It’s all going well (although from what I can make out in the book it would indicate that the code does not jump down to the text but hit point 4 first) it’s this bit, (point 4) that gives me the problems- to my thinking at this stage we would have a string like this :
<element node><firstchildnode>FIRST CHILD TEXT
As this post is already very long I’ll just suffice by asking if someone can explain the code @ point 4 better for me. Is it checking that the last node is an element & adding the closing tag before adding the text?
By lastChild does it mean the closing </> of that particular element?
Thanks for any help.
PART OF A FUNCTION CALLED “tostring()”
var xStr = "";
XMLNode.spaces += 4;
1. if (this.nodetype == 1) {
//it's an element; check its kids
xStr += spaceOut()+"<"+this.nodeName;
var attr = this.attributes;
for (var eachAttr in attr){
xStr += " " + eachAttr+"='"+this.attributes[eachAttr]+"'";
}
xStr +=">";
2. if (this.firstChild.nodeType == 1){xStr += "\n";}
var chlength = this.childNodes.length;
for (var i=0;i < chlength; i++) {
xStr += this.childnodes[i].toString();
}
4. if (this.lastChild.nodeType==1){xStr += spaceOut();}
xStr += "</"+this.nodeName+">\n";
XMLNode.spaces -= 4;
} else {
//it's text
3. xStr += this.nodeValue;
XMLNode.spaces -= 4;
}
return(xStr);
}
I have been working through some examples in a XML course book & have become totally stuck on one piece of code.
Although I understand 98% of what the code is trying to achieve, which, to surmise is to portray an XML document stripped of whitespace & properly spaced, I can not get my head around one particular section.
Set out below is the relevant portion of a larger segment of code (See attachment for full code) The portion in red is where I come unstuck.( The numbers set out in blue are not parts of the code but used to indicate parts of relevance in relation to the post.)
My understanding is the code is recursive so calls itself from within the main code digging deeper into the XML txt file, so if we take things from the function being called on the root element – At point 1. we know that the root element is nodeType 1 so it spaces it out, adds the name & attributes, no problem.
We know that the firstChild node is type 1 so the scripted adds a break and then calls the function recursively on all the childNodes ChildNodes, childNodes etc. etc.
Now if the firstChild only has one set of children, the function would be called on this child, which as the next node in this child would be text, at point 2, we would drop down to the “text section” (point 3.) & populate the string with the nodeValue.
It’s all going well (although from what I can make out in the book it would indicate that the code does not jump down to the text but hit point 4 first) it’s this bit, (point 4) that gives me the problems- to my thinking at this stage we would have a string like this :
<element node><firstchildnode>FIRST CHILD TEXT
As this post is already very long I’ll just suffice by asking if someone can explain the code @ point 4 better for me. Is it checking that the last node is an element & adding the closing tag before adding the text?
By lastChild does it mean the closing </> of that particular element?
Thanks for any help.
PART OF A FUNCTION CALLED “tostring()”
var xStr = "";
XMLNode.spaces += 4;
1. if (this.nodetype == 1) {
//it's an element; check its kids
xStr += spaceOut()+"<"+this.nodeName;
var attr = this.attributes;
for (var eachAttr in attr){
xStr += " " + eachAttr+"='"+this.attributes[eachAttr]+"'";
}
xStr +=">";
2. if (this.firstChild.nodeType == 1){xStr += "\n";}
var chlength = this.childNodes.length;
for (var i=0;i < chlength; i++) {
xStr += this.childnodes[i].toString();
}
4. if (this.lastChild.nodeType==1){xStr += spaceOut();}
xStr += "</"+this.nodeName+">\n";
XMLNode.spaces -= 4;
} else {
//it's text
3. xStr += this.nodeValue;
XMLNode.spaces -= 4;
}
return(xStr);
}