Problems with code example from XML reference book

[font=Times New Roman]Hi,[/font]

[font=Times New Roman]I have been working through some examples in a XML course book & have become totally stuck on one piece of code.[/font]

[font=Times New Roman]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.[/font]

[font=Times New Roman]Set out below is the relevant portion of a larger segment of code (See attachment for full code) The portion in [color=#ff0000]red[/color] 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.)[/font]

[font=Times New Roman]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.[/font]

[font=Times New Roman]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.[/font]

[font=Times New Roman]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.[/font]

[font=Times New Roman]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 :[/font]

[font=Times New Roman]<element node><firstchildnode>FIRST CHILD TEXT[/font]

[font=Times New Roman]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?[/font]

[font=Times New Roman]By lastChild does it mean the closing </> of that particular element?[/font]

[font=Times New Roman]Thanks for any help.[/font]

[font=Times New Roman]PART OF A FUNCTION CALLED “tostring()”[/font]

[font=Times New Roman][font=Times New Roman]var xStr = “”;[/font]

[font=Times New Roman]XMLNode.spaces += 4;[/font]

[font=Times New Roman][size=3][color=blue]1[/color][/size]. if (this.nodetype == 1) {[/font]

[font=Times New Roman]//it’s an element; check its kids [/font]

[font=Times New Roman]xStr += spaceOut()+"<"+this.nodeName;[/font]

[font=Times New Roman]var attr = this.attributes;[/font]

[font=Times New Roman]for (var eachAttr in attr){[/font]

[font=Times New Roman]xStr += " " + eachAttr+"=’"+this.attributes[eachAttr]+"’";[/font]

[font=Times New Roman]}[/font]

[font=Times New Roman]xStr +=">";[/font]

[font=Times New Roman][size=3][color=blue]2.[/color][/size] if (this.firstChild.nodeType == 1){xStr += "
";}[/font]

[font=Times New Roman]var chlength = this.childNodes.length;[/font]

[font=Times New Roman]for (var i=0;i < chlength; i++) {[/font]

[font=Times New Roman]xStr += this.childnodes*.toString();[/font]

[font=Times New Roman]} [/font]
[font=Times New Roman][size=3][color=blue]4.[/color][/size] [color=red]if (this.lastChild.nodeType==1){xStr += spaceOut();}[/color][/font]

[font=Times New Roman][color=red]xStr += “</”+this.nodeName+">
";[/color][/font]

[font=Times New Roman][color=red]XMLNode.spaces -= 4;[/color][/font]

[font=Times New Roman]} else {[/font]

[font=Times New Roman]//it’s text[/font]

[font=Times New Roman][size=3][color=blue]3.[/color][/size] xStr += this.nodeValue;[/font]

[font=Times New Roman]XMLNode.spaces -= 4;[/font]

[font=Times New Roman]}[/font]

[font=Times New Roman]return(xStr);[/font]

[font=Times New Roman]}[/font]
[/font]