
Welcome Guest
|
#1
|
|||
|
|||
|
Problems with XML code from reference book.
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()” Actionscript:
var xStr = "";
XMLNode.spaces += 4;
[COLOR=Navy]1.[/COLOR] 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 +=">";
[COLOR=Navy]2.[/COLOR] if (this.firstChild.nodeType == 1){xStr += "\n";}
var chlength = this.childNodes.length;
for (var i=0;i < chlength; i++) {
xStr += this.childnodes[i].toString();
}xStr += "</"+this.nodeName+">\n"; XMLNode.spaces -= 4; } Actionscript:
else {
//it's text
[COLOR=Navy]3.[/COLOR] xStr += this.nodeValue;
XMLNode.spaces -= 4;
}
return(xStr);
} |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Code for XML file is tracing 'undefined'...why? | lariat1997 | XML & Sockets | 1 | 03-08-2007 10:09 PM |
| Flash reference book for newbies | amagab | Newbies |
3 | 12-07-2004 08:43 PM |
| xml book | mouse | XML & Sockets | 12 | 04-28-2003 05:42 AM |
| flash and xml | mobileMX | XML & Sockets | 0 | 03-04-2003 05:50 AM |
| flash and xml | mobileMX | Newbies |
0 | 03-04-2003 05:44 AM |

Other Programming



