Jack Roscoe
Jack Roscoe

Reputation: 4313

Having trouble parsing XML with jQuery

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?

Upvotes: 1

Views: 488

Answers (2)

Matthew Flaschen
Matthew Flaschen

Reputation: 284796

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.

Upvotes: 1

user113716
user113716

Reputation: 322492

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

Upvotes: 1

Related Questions