Jquery/Ajax - XML 解析 - 根据标准提取记录



是否可以从 xml 中提取与查询字符串条件匹配的记录?

例如,我有以下解析 xml 文件的解析脚本,页面 url 可以包含几个不同的查询字符串,例如音乐 - 是否可以读取查询字符串,然后只解析与查询字符串中的短语匹配的记录?

$.ajax({
    type: "GET",
url: "#",
dataType: "xml",
success: function(xml) {
    $(xml).find("catalogueResult").each(function(i) {
        var mediaArtist = $("artist", this).text()
        var imgSrc   = $("imageUrl", this).text()
        var title = $("title", this).text()
        var priceCode = $("priceCode", this).text()
        var mediaRow= $('<div class="mediaBlock"><div class="promoImg floL"><img src="'+imgSrc+'" width="75" alt="'+title+'"/></div><div class="promoContent"><h2>'+title+'</h2><h2 class="red">'+mediaArtist+'</h2><div class="buyBtn"><span><A href="http://www.mobilechilli.com">Buy Now</a></span></div></div></div>');
        var compiledData = $(mediaRow);
        $(".recommends").append(compiledData);
    });
}

});

尝试这样的事情。尽管您可能必须根据构建 xml 的方式更改某些变量。

$(document).ready(function(){
$.ajax({
  type: "GET",
  url: "( URL to your xml )",
  dataType: "xml",
  success: parseXml
});

// function that parses XML
function parseXml(xml){
    // find node with name "catalogueResult" and run function for each
    $(xml).find("catalogueResult").each(function(){
    // variables
    var mediaArtist = $(this).find("artist").text();
    var imgSrc = $(this).find("imageUrl").text();
    var title = $(this).find("title").text();
    var priceCode = $(this).find("priceCode").text();
    var mediaRow= '<div class="mediaBlock"><div class="promoImg floL"><img src="'+imgSrc+'" width="75" alt="'+title+'"/></div><div class="promoContent"><h2>'+title+'</h2><h2 class="red">'+mediaArtist+'</h2><div class="buyBtn"><span><A href="http://www.mobilechilli.com">Buy Now</a></span></div></div></div>';
    $(".recommends").append(mediaRow);
    });
}

最新更新