Dymond
Dymond

Reputation: 2277

Loop inside loop?

I have created a loop in JavaScript that reads out the name of my nodes and the value inside.

The loop works for the first node and the second one, but on the rest of the nodes it's just repeating the value of the second node.

So the output gets like this:

 Name on nod 1 is title
 The value in the node is XML Content and Data

 Name on nod 2 is Author
 The value in the node is XML Content and Data

Et cetera.

Should I create a loop inside the loop? Ir can I multiloop the entire tree?

if (xmlDoc.parseError != 0) {
    alert("Error Code: " + xmlDoc.parseError.errorCode + "\n"
        + "Error Reason: " + xmlDoc.parseError.reason + "\n"
        + "Error Line: " + xmlDoc.parseError.line)
}
root = xmlDoc.documentElement
rootList = root.childNodes
len = rootList.length

x = xmlDoc.getElementsByTagName("title")[0]
y = x.childNodes[0];

for (i = 0; i < len; i++) {
    j = i + 1
    document.write("Name on nod " + j + " is " + rootList.item(i).nodeName + "<br />")
    document.write(" value of the the nod is  " + y.nodeValue + "<br />" + "<br />");
}

Upvotes: 0

Views: 170

Answers (2)

Sebastian
Sebastian

Reputation: 2279

y is always pointing to the same node (y= x.childNodes[0]). You probably intend to get a different node in each iteration of the loop.

Upvotes: 1

Aman
Aman

Reputation: 153

Log value of i in the loop. If you see it repeating then JavaScript for loop index strangeness can probably help you.

Upvotes: 0

Related Questions