用属性提取XML属性

Extract XML attribute with property

本文关键字:属性 XML 提取      更新时间:2023-09-26

以下XML对象:

http://ws.audioscrobbler.com/2.0/user/christiaan123/topalbums.xml

返回'image'属性下的专辑封面,但有'size'属性。我如何使用javascript返回<image size="extralarge>的url ?

到目前为止,我已经试过了:

    if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // for IE 5/6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 xhttp.open("GET","http://ws.audioscrobbler.com/2.0/user/christiaan123/topalbums.xml",false);
 xhttp.send();
 lastFmData=xhttp.responseXML;
 albums = lastFmData.getElementsByTagName("album")
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // for IE 5/6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 xhttp.open("GET","http://ws.audioscrobbler.com/2.0/user/christiaan123/topalbums.xml",false);
 xhttp.send();
 lastFmData=xhttp.responseXML;
 albums = lastFmData.getElementsByTagName("album")
 cover = []
 url = []
 var divs = document.getElementsByTagName("img");
 console.log(divs);
 for (i=0; i<50; i++) {
    cover[i] = albums[i].children[8].innerHTML;
    url[i] = albums[i].children[3].innerHTML;
    divs[i].src = cover[i];
 }