Results 1 to 9 of 9

Thread: javascript for loop and XML

  1. #1
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default javascript for loop and XML

    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.
    Code:
    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.

  2. #2
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Anybody?

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jan 2007
    Location
    Charlotte, NC
    Posts
    82
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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)).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    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...
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •