9

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.

5
  • As far as I know, there's no iteration limit on each(). What does $(xml).find('Table').length return? Commented Aug 15, 2011 at 21:13
  • Are you sure you have more elements than that in there? Stick a console.log($(xml).find('Table').length) in there and make triple sure. Commented Aug 15, 2011 at 21:14
  • It's giving me 32. So it's not the .each() that's doing it, but there are close to 1000 elements in the XML file. Why would only 32 be returned? Commented Aug 15, 2011 at 21:18
  • 3
    Look at the format of the XML file. Is there a syntax error after the 31st or 32nd entry? Maybe a rogue invalid character? What is generating this XML file? Commented Aug 15, 2011 at 21:22
  • 1
    Yeah just figured it out, at least that bit. There were & in the file, so I had to replace those with &amp; Thanks for the help. Commented Aug 15, 2011 at 21:23

1 Answer 1

19

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");
    }
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That's exactly what I was looking for, after I found the XML document had errors. It would be impossible for me to fix it everytime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.