SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Nested for () starement

    I don't know why this has eluded me and I hope someone can help me out here. I have a for statement and I have found a match:

    Code:
    if (xmlObj.childNodes[1].childNodes[1].childNodes[3].childNodes[i].tagName == "offerGroup")
    			{
    				alert("length = "+xmlObj.childNodes[1].childNodes[1].childNodes[3].childNodes[i].tagName);
    				//for (j = 1; j < xmlObj.childNodes[1].childNodes[1].childNodes[3].childNodes[i].length; j++)
    				//{
    					var MainModule_offer[j]Text = xmlObj.childNodes[1].childNodes[1].childNodes[3].childNodes[i].childNodes[1].getAttribute("text");
    					var MainModule_offer[j]Blurb = xmlObj.childNodes[1].childNodes[1].childNodes[3].childNodes[i].childNodes[1].getAttribute("blurb");
    					var MainModule_offer[j]Link = xmlObj.childNodes[1].childNodes[1].childNodes[3].childNodes[i].childNodes[1].getAttribute("link");
    				//}
    			}
    In this case I have 4 matches for "offerGroup". There is a childNode, childNode[1], of the offerGroup node that contains three attributes that I am extracting. Now that I have captured the node offerGroup how do I iterate through it so I can name the 3 vars that capture the 3 attributes of each offerGroup node. Once again, 4 nodes.

    Help!

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about:
    Code:
    var node = xmlObj.childNodes[1].childNodes[1].childNodes[3].childNodes[i].childNodes[1];
    for (var a in node.attributes) {
     var MainModule_offer[j][a] = node[a];
    }
    I don't know what your XML document looks like, so this is an untested guess.

    On a side note, why are you so verbose? Have you considered using 'shortcut' variables to avoid all that typing?
    var me = null;


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
  •