使用jQuery .each遍历XML文件 [英] Using jQuery .each to loop over XML file

查看:241
本文介绍了使用jQuery .each遍历XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当长的XML文件。下面是我用来检索文件,然后使用jQuery的.each()浏览文件的代码,输出正确的信息:

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>');
        });
    }
});
});

我遇到的问题是代码仅使我达到了元素31。我添加了索引变量来查看,它给了我0到30的索引。所以有一些限制,.each()仅上升到30的索引,如果是这样,还有另一种遍历XML的方法吗文件?谢谢。

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.

编辑:已经解决,至少现在是这样。 XML文件中包含& ;,用于阻止处理。我猜想另一个提醒是先验证您的源文件。

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.

推荐答案

尝试使用 parseXML 在找到元素之前

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");
    }
  });
});

这篇关于使用jQuery .each遍历XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆