案例:XML案例     状态:可编辑再运行    进入竖版
 运行结果 
x
 
1
<html>
2
<head>
3
<script type="text/javascript">
4
if (window.XMLHttpRequest)
5
  {// code for IE7+, Firefox, Chrome, Opera, Safari
6
  xmlhttp=new XMLHttpRequest();
7
  }
8
else
9
  {// code for IE6, IE5
10
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
11
  }
12
xmlhttp.open("GET","/example/xmle/cd_catalog.xml",false);
13
xmlhttp.send();
14
xmlDoc=xmlhttp.responseXML; 
15
x=xmlDoc.getElementsByTagName("CD");
16
17
function displayCDInfo(i)
18
{
19
artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
20
title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
21
year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
22
country=(x[i].getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue);
23
company=(x[i].getElementsByTagName("COMPANY")[0].childNodes[0].nodeValue);
24
price=(x[i].getElementsByTagName("PRICE")[0].childNodes[0].nodeValue);
25
txt="Artist: "+artist+"<br />Title: "+title+"<br />Year: "+year+"<br />Country: "+country+"<br />Company: "+company+"<br />Price: "+price  ;
26
document.getElementById("showCD").innerHTML=txt;
27
}
28
</script>
29
</head>
30
31
<body>
32
<div id='showCD'>点击某个 CD 就可显示专辑信息:</div><br />
33
<script type="text/javascript">
34
document.write("<table border='1'>");
35
for (var i=0;i<x.length;i++)
36
  { 
37
  document.write("<tr onclick='displayCDInfo(" + i + ")'>");
38
  document.write("<td>");
39
  document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
40
  document.write("</td><td>");
41
  document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
42
  document.write("</td></tr>");
43
  }
44
document.write("</table>");
45
</script>
46
47
</body>
48
</html>