Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery .each to loop over XML file

Tags:

jquery

xml

I have an XML file that is pretty long. Below is the code I am using to retrieve the file and then go through the file using jQuery's .each(), outputting the correct information:

$(document).ready(function(){
$.ajax({
    type: "GET",
    url: "data.xml",
    dataType: "xml",
    success: function(xml) {
        $(xml).find('Table').each(function(index){
            var provider = $(this).find('Provider').text();
            var channel = $(this).find('FeedCommonName').text();
            var hd = $(this).find('FeedIsHD').text();
                $('.box ul').append('<li>'+channel+'</li>');
        });
    }
});
});

The problem I'm having is the code only gives me up to element 31. I added the index variable in to see that, and it is giving me an index from 0 to 30. So is there some limitation that .each() only goes up to an index of 30, and if so, is there another way to go through the XML file? Thanks.

EDIT: Solved, at least for now. There were &'s in the XML file, which was holding up the processing. I guess another reminder to validate your source file first.

like image 897
Darin Avatar asked Aug 15 '11 21:08

Darin


1 Answers

Try using parseXML before you find the element

$(document).ready(function(){
  $.ajax({
    type: "GET",
    url: "data.xml",
    dataType: "xml",
    success: function(xml) {
        $.parseXML(xml).find('Table').each(function(index){
            var provider = $(this).find('Provider').text();
            var channel = $(this).find('FeedCommonName').text();
            var hd = $(this).find('FeedIsHD').text();
            $('.box ul').append('<li>'+channel+'</li>');
        });
    },
    error: function() {
        $('.box ul').text("Failed to get xml");
    }
  });
});
like image 184
ShankarSangoli Avatar answered Oct 23 '22 20:10

ShankarSangoli