PDA

View Full Version : javascript for loop and XML



jamiller
04-20-2007, 02:37 PM
So I'm using XML in my html doc to populate some fields with data.

The problem is I don't want to create document.getElementById("IDNAME").innerHTML=xmlDoc.getElementsByTagName("XMLNODENAME")[0].childNodes[0].nodeValue; for each span id I have so I have decided to make a for loop. The problem with the code I have now is that it only populates the first id.


var xmlDoc;
function loadXML() {
// XML Windows parser
if(window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("test.xml")
getXML();
// Firefox XML parser
} else if (document.implementation && document.implementation.createDocument) {
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("test.xml")
xmlDoc.onload=getXML;
}
}

function getXML() {
// 30 is the number of nodes; TT is the span id (plus a number); and item is the XML node (plus a number)
for(var i=1; i<=30; i++) {
document.getElementById('TT'+i).innerHTML = xmlDoc.getElementsByTagName('item'+i)[i-1].childNodes[i-1].nodeValue;
}
}

Like I said, span id TT1 populates no problem, but spans 2-30 do not. Any suggestions? I cannot figure this out.

jamiller
04-20-2007, 10:30 PM
Anybody?

Twey
04-20-2007, 10:49 PM
Does any error fire? Are the nodes actually being populated, but with no data, or are they never being touched? Also, you should avoid innerHTML: it's non-standard and doesn't always work as one might expect.

jamiller
04-20-2007, 11:52 PM
Well it's funny. Only one node is being populated, the first one. The rest are not. From my experiences with for loops, mainly in Flash, if something is wrong but kinda working then it was always the last item in the loop, never the first.

In IE the error (bottom-left of the window) says error on line 152, object required. This makes no sense because line 152 is a commented out line right below the document.getElementsById line in the for loop (I took out this comment when I copied the code here). Is there another way to check for an error that I'm unaware of?

And I agree with the innerHTML. I hate it. But I don't know of any other way to populate a field with xml data.

I should also point out that I am a front-end developer. I mainly work in Photoshop, CSS, HTML, and basic Javascript so my coding knowledge isn't very advanced although I am learning.

Twey
04-21-2007, 12:10 AM
Well, the first thing you need to do is to abandon IE's rather pathetic script debugger. Either install Microsoft Visual Studio, which includes a better Javascript debugger, or install Firefox and use that to debug (although of course this doesn't really help for IE-only problems). I find Firefox' error console much nicer to debug with than that of other browsers, especially with the Firebug extension, although Opera's sometimes picks up problems that Firefox' misses.

jamiller
04-21-2007, 01:32 AM
Well then. I'm building the site in Visual Studio so I'll give that a try. I'm having the same problem in Firefox but will use the Firefox debug method to see if it tells me anything. Thanks for the comments.

Twey
04-21-2007, 01:50 AM
IE's script debugger reports errors very unhelpfully, in ways such as, as you've noticed, specifying completely inaccurate line numbers (although there is some way to work it out; if I remember correctly, IE's script debugger takes all external script files, joins them together, adds in the HTML file, then gives the line number as it exists in this fictitious file rather than the real file(s)).

boxxertrumps
04-21-2007, 04:51 AM
opera is thebest browser to test in, it's currently the most standards compliant.
Best CSS and JS around.
hope it's available for linux.
it appears to be available for most linux distros...

Twey
04-21-2007, 01:33 PM
Last I checked it was a little worse than Konqueror (and thus probably Safari too). It's available for all Linux distributions, but only as a 32-bit binary.