2012-05-16 40 views
0

是否可以從xml中提取匹配查詢字符串條件的記錄?Jquery/Ajax - 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); 

    }); 
} 

});

+0

我注意到你正在做這些'$(''');'在字符串之前?我不認爲你必須這樣做。你還無故添加了'(i)'。 –

回答

0

嘗試這樣的事情。雖然你可能不得不根據你如何構建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); 

    }); 
}