使用属性提取XML属性

时间:2014-05-18 13:30:46

标签: javascript xml

以下XML对象:

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

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

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

    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")

1 个答案:

答案 0 :(得分:0)

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];
 }