views:

92

answers:

2

Hi Guys,

I'm trying to parse some XML data using jQuery, and as it stands I have extracted the 'ID' attribute of the required nodes and stored them in an array, and now I want to run a loop for each array member and eventually grab more attributes from the notes specific to each ID.

The problem currently is that once I get to the 'for' loop, it isn't looping, and I think I may have written the xml path data incorrectly. It runs once and I recieve the 'alert(arrayIds.length);' only once, and it only loops the correct amount of times if I remove the subsequent xml path code.

Here is my function:

    var arrayIds = new Array();
$(document).ready(function(){
  $.ajax({
    type: "GET",
    url: "question.xml",
    dataType: "xml",
    success: function(xml)
    {
                  $(xml).find("C").each(function(){
                        $("#attr2").append($(this).attr('ID') + "<br />");
                        arrayIds.push($(this).attr('ID'));
                  });

                  for (i=0; i<arrayIds.length; i++)
                  {
                    alert(arrayIds.length);
                    $(xml).find("C[ID='arrayIds[i]']").(function(){
                        // pass values
                        alert('test');
                    });
                  }
    }
  });
});

Any ideas?

+1  A: 

It should be:

$(xml).find("*[ID=" + arrayIds[i] + "]").each(function(){
    // pass values
    alert('test');
});

Before you were looking for an id with the literal value "arrayIds[i]". Also, ids are unique, so you don't need the C, and I changed it to standard jQuery syntax. Also, as Patrick said, you were missing each.

Matthew Flaschen
After ammending my code to your suggestion, there was no change in behaviour and my Error Console prompted me with 'XML filter is applied to non-XML value...'.ADDED: I have added the 'each', which again like Patrick's answer causes the loop to function correctly, however I still have trouble executing the function inside the loop to trigger the second alert.Any ideas?I'm not personally familiar with this error.
Jack Roscoe
`'#'+arrayIds[i]` relies on the parser knowing that the attribute called `ID` is supposed to be of schema type `ID`. For HTML documents that will be the case, but for XML it will (usually) not. The `[ID=...]` attribute selector *is* necessary.
bobince
Thanks, @bobince. My mistake.
Matthew Flaschen
Your latest update seems to have fixed the issue. Thanks to everyone who commented for the help.
Jack Roscoe
+1  A: 

This line is invalid. You're missing a function name. This is crashing the script.

$(xml).find("C[ID='arrayIds[i]']").(function(){

should be (perhaps):

$(xml).find("C[ID='" + arrayIds[i] + "']").each(function(){ // Note the added each
patrick dw
Your suggestion did cause the 'alert(arrayIds.length);' to loop and run the correct number of times, however the 'alert('test');' inside the inner function did not execute for some reason.
Jack Roscoe
Your selector is incorrect too. I'll update my answer.
patrick dw
Matthews latest version seemed to fixed the problem. Thanks very much for your help also though.
Jack Roscoe
@Jack - Uh, indeed it does...
patrick dw
Oh sorry, I think I tested a slight older version of your post. Your fix works correctly (as I'm sure you know).Both fixes solve the original, however Matthews post causes the second alert to run a large amount of times due the '*' replacing the 'C' in the XML Path.The behaviour caused by the '*' I do not fully understand but it seemed incorrect.
Jack Roscoe
@Jack - `*` is a wildcard selector, for HTML anyway. The reason I didn't concatenate the `arrayIds[i]` in my original answer, was that Matthew did in his, and I didn't want it to seem as though I was copying from his answer, so I referenced his. Then he changed his answer to take from mine the actual cause of the crash, so I updated mine in response.
patrick dw