我正致力于从last.fm服务器获取艺术家的信息,我需要能够阅读他们在服务器上拥有的艺术家的图片。您可以查看示例XML返回语句here。如您所见,每位艺术家都有很多图像(小,中,大,超大和巨型)。我需要能够准备好任何这些并获得适当的价值。
我是通过Javascript阅读XML / DOM的新手,所以我确信它相当简单,但是" size"的额外属性。在XML文件中让我有点循环。谢谢!
答案 0 :(得分:1)
这样可以获得第一张图片。
$.ajax({
url: "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=ac74a1e6fae52bc5f55a8a6f30b84db9",
type: "GET",
dataType: "html",
success: function(data) {
var xml = $.parseXML(data)
$(xml).each(function()
{
alert($(this).find("lfm>artist>image[size]:first").text());
});
},
error: function(jqXHR, textStatus, errorThrown ){
// debug here
alert("failure");
}
});
}
答案 1 :(得分:0)
使用$ .parseXML创建一种x文档..然后你可以用jquery选择器找到它的东西。
getXMLNode(retObj.responseXML, "thatField");
function getXMLNode(xml, nameOfField) {
return $($.parseXML(xml)).find(':first').find(nameOfField).text();
}
答案 2 :(得分:0)
此效果应返回所有图像尺寸:
$.ajax({
type: "GET",
url: "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=ac74a1e6fae52bc5f55a8a6f30b84db9",
dataType: "xml",
success: function (xml) {
// Parse the xml file and get data
var xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc);
$xml.find('image').each(function () {
console.log($(this).text())
});
}
});