如何使用Javascript从xml显示图像?

时间:2018-05-21 12:31:30

标签: javascript html xml

如何添加新标签,并显示xml数据中的图像相册?

如果getElementsByTagName出现任何问题?

我的HTML代码。

<!DOCTYPE html>
<html>
<style>
table,th,td {
  border : 1px solid black;
  border-collapse: collapse;
}
th,td {
  padding: 5px;
}
</style>
<body>

<h1>The Best Century Album</h1>

<button type="button" onclick="loadDoc()">Click Here</button>
<br><br>
<table id="demo"></table>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myFunction(this);
    }
  };
  xhttp.open("GET", "ane.xml", true);
  xhttp.send();
}
function myFunction(xml) {
  var i;
  var xmlDoc = xml.responseXML;
  var table="<tr><th>Artist</th><th>Title</th><th>Country</th><th>Company</th><th>Price</th><th>Year</th><th>Image</th></tr>";
  var x = xmlDoc.getElementsByTagName("CD");
  for (i = 0; i <x.length; i++) { 
    table += "<tr><td>" +
    x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
    "</td><td>"+
    x[i].getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("COMPANY")[0].childNodes[0].nodeValue +
    "</td><td>"+
    x[i].getElementsByTagName("PRICE")[0].childNodes[0].nodeValue +
    "</td><td>"+
    x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue +
    "</td><td>"+
    x[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue +
    "</td></td>";
  }
  document.getElementById("demo").innerHTML = table;
}
</script>

</body>
</html>

我的XML

<?xml version="1.0" encoding="UTF-8"?>
<CATALOG xmlns:xlink="http://www.w3.org/1999/xlink">
  <CD>
    <TITLE>American Idiot</TITLE>
    <ARTIST>Green Day</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>2009</YEAR>
    <IMAGE> <xlink:type="simple"
  xlink:href="/images/aigd.jpg"
  xlink:show="new"/></IMAGE>
  </CD>
  <CD>
    <TITLE>Holiday</TITLE>
    <ARTIST>Green Day</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>2009</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
  <CD>
    <TITLE>Wake Me Up When September End</TITLE>
    <ARTIST>Green Day</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>RCA</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>2009</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
  <CD>
    <TITLE>I Wanna Hold Your Hand</TITLE>
    <ARTIST>The Beatles</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Virgin records</COMPANY>
    <PRICE>10.20</PRICE>
    <YEAR>1960</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
  <CD>
    <TITLE>Please Please Me</TITLE>
    <ARTIST>The Beatles</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>BMG</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1960</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
  <CD>
    <TITLE>Hidup Di Bui</TITLE>
    <ARTIST>Koes Plus</ARTIST>
    <COUNTRY>Indonesia</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1965</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
  <CD>
    <TITLE>Bis Sekolah</TITLE>
    <ARTIST>Koes Plus</ARTIST>
    <COUNTRY>Indonesia</COUNTRY>
    <COMPANY>CBS</COMPANY>
    <PRICE>8.10</PRICE>
    <YEAR>1965</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
  <CD>
    <TITLE>Dengarkan Rani</TITLE>
    <ARTIST>Sheila On 7</ARTIST>
    <COUNTRY>Indonesia</COUNTRY>
    <COMPANY>Pickwick</COMPANY>
    <PRICE>8.50</PRICE>
    <YEAR>2000</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
  <CD>
    <TITLE>Melompat Lebih Tinggi</TITLE>
    <ARTIST>Sheila On 7</ARTIST>
    <COUNTRY>Indonesia</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>10.80</PRICE>
    <YEAR>2005</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
  <CD>
    <TITLE>Nirwana</TITLE>
    <ARTIST>Gigi</ARTIST>
    <COUNTRY>Indonesia</COUNTRY>
    <COMPANY>Atlantic</COMPANY>
    <PRICE>8.70</PRICE>
    <YEAR>1998</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
  <CD>
    <TITLE>Nakal</TITLE>
    <ARTIST>Gigi</ARTIST>
    <COUNTRY>EU</COUNTRY>
    <COMPANY>Mega</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>2005</YEAR>
    <IMAGE>x</IMAGE>
  </CD>
</CATALOG>

抱歉我的英语不好。 请帮我。 感谢

0 个答案:

没有答案