[font=Times New Roman]I have been having a lot of trouble understanding a code example from an XML reference book. The problem is not really XML specific but more to do with how Flash handles “loops” and “if” statements.[/font]
[font=Times New Roman]I’d just state for those not up to speed with the only bit of XML you would need to know for this example, that XML nodes can have two values 1=element node and 2=text node.[/font]
[font=Times New Roman]What this code does is loop through an XML document and add spacing to it so each child heading is indented from it’s parent.[/font]
[font=Times New Roman]As I say this has little if nothing to do with XML, but to make the post clearer I will use the following XML file to show where I’m at with this:[/font]
[font=Times New Roman][color=blue]<book name=‘XML in Flash’>[/color][/font]
[color=blue] [/color]
[font=Times New Roman][color=blue]<chapter name=‘1’>Overview of XML in Flash</chapter>[/color][/font]
[color=blue] [/color]
[font=Times New Roman][color=blue]<chapter name=‘2’>The Details of XML</chapter>[/color][/font]
[color=blue] [/color]
[font=Times New Roman][color=blue]<chapter name=‘3’>Getting Your Feet Wet</chapter>[/color][/font]
[color=blue] [/color]
[font=Times New Roman][color=blue]<chapter name=‘4’>Using XML Data in Flash</chapter>[/color][/font]
[color=blue] [/color]
[font=Times New Roman][color=blue]</book>[/color][/font][color=blue] [/color]
[font=Times New Roman]Now I can understand everything about the main code below until we get to this section [/font]
[font=Times New Roman][color=red]if (this.lastChild.nodeType==1){xStr += spaceOut();}[/color][/font]
[color=red] [/color]
[font=Times New Roman][color=red]xStr += “</”+this.nodeName+">
";[/color][/font]
[color=red] [/color]
[font=Times New Roman][color=red]XMLNode.spaces -= 4;[/color][/font]
[color=red] [/color]
[font=Times New Roman][color=red]}[/color][/font]
[font=Times New Roman]which is set out in red below in context to actual function.[/font]
[font=Times New Roman]From example below you test the nodes value if it =1 you put the node name including<> tags & attributes you then call a recursive function on any childNodes that the document may have so using the above XML file we will have:[/font]
[font=Times New Roman]<book name=‘XML in Flash’>[/font]
[font=Times New Roman]<chapter name=‘1’>[/font]
[font=Times New Roman]the first time the loop operates – NOW what happens next? Does the code skip the next if statement about the closing tag & go to the text node section? And if so how does this element get it’s closing tag? The only way I can see that the “</”+this.nodeName+">
"; if section fires is if the current element has a child if (this.lastChild.nodeType==1)[/font]
[font=Times New Roman]In a nut shell I don’t understand how the code provides the closing “</”+this.nodeName+"> tags for all the chapter elements.[/font]
[font=Times New Roman]Any help please.[/font]
[font=Times New Roman]CODE[/font]
[font=Times New Roman]XMLNode.prototype.toString = function () {[/font]
[font=Times New Roman]function spaceOut() {[/font]
[font=Times New Roman]var sStr = “”;[/font]
[font=Times New Roman]for(var i=0;i<XMLNode.spaces;i++){[/font]
[font=Times New Roman]sStr += " ";[/font]
[font=Times New Roman]}[/font]
[font=Times New Roman]return(sStr);[/font]
[font=Times New Roman]}[/font]
[font=Times New Roman]var xStr = “”;[/font]
[font=Times New Roman]XMLNode.spaces += 4;[/font]
[font=Times New Roman]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]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][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][color=red]} [/color]else {[/font]
[font=Times New Roman]//it’s text[/font]
[font=Times New Roman]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]