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.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 3=text node.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.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:

Actionscript:
XMLNode.prototype.toString = function () {
function spaceOut() {
var sStr = \"\";
for(var i=0;i<XMLNode.spaces;i++){
sStr += \" \";
}
return(sStr);
}
var xStr = \"\";
XMLNode.spaces += 4;
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 +=\">\";
if (this.firstChild.nodeType == 1){xStr += \"\n\";}
var chlength = this.childNodes.length;
for (var i=0;i < chlength; i++) {
xStr += this.childnodes[i].toString();
}
if (this.lastChild.nodeType==1){xStr += spaceOut();}
xStr += \"</\"+this.nodeName+\">\n\";
XMLNode.spaces -= 4;
} else {
//it's text
xStr += this.nodeValue;
XMLNode.spaces -= 4;
}
return(xStr);
}