Jquery xml解析读取XML一次

时间:2013-03-31 05:08:57

标签: jquery xml-parsing

示例来自:http://papermashup.com/parse-xml-with-jquery/

函数xmlParser(xml) 在成功调用时工作正常。

我想写另一个 *函数xmlParser_selective(选择参数)*

这是否意味着我必须通过 $重新读取xml.ajax({type:“GET”,url:“books.xml”,dataType:“xml”,success:xmlParser}); ? 我的目标是编写一个可以根据请求解析一些选择性节点的函数,但我不想再读取xml,因为我已经在文档就绪时完成了它。 我不能让它似乎工作。任何想法? 功能xmlParser_selective(选择参数)??

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "books.xml",
        dataType: "xml",
        success: xmlParser
    });
});

function xmlParser(xml) {

    $('#load').fadeOut();

    $(xml).find("Book").each(function () {

        $(".main").append('<div class="book"><div class="title">' + $(this).find("Title").text() + '</div><div class="description">' + $(this).find("Description").text() + '</div><div class="date">Published ' + $(this).find("Date").text() + '</div></div>');
        $(".book").fadeIn(1000);

    });

}

function xmlParser_selective(selection,parameters) {
    $(xml).find("Book").each(function () {
    // this is not working.How do i get to  $(xml) ? that is read on document ready
    });
}

1 个答案:

答案 0 :(得分:0)

您无需再次加载xml ..

   var my_xml;
   $.ajax({
        type: "GET",
        url: "books.xml",
        dataType: "xml",
        success: function(data){
          my_xml = data;
        }
    });



function xmlParser_selective(selection,parameters) {
    $(my_xml).find("Book").each(function () {
    // this is not working.How do i get to  $(xml) ? that is read on document ready
    });
}