XML技術手冊登入
XML技術手冊
作者:php.cn  更新時間:2022-04-14 15:57:53

XML/HTML


HTML 頁面顯示XML 資料


#在HTML 頁面中顯示XML 資料

在下面的實例中,我們打開一個XML 檔案("cd_catalog.xml"),然後遍歷每個CD 元素,並顯示HTML 表格中的ARTIST 元素和TITLE 元素的值:

實例

<html>
<body>

<script>
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.open("GET","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
 { 
 document.write("<tr><td>");
 document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
 document.write("</td><td>");
 document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
 document.write("</td></tr>");
 }
document.write("</table>");
</script>

</body>
</html>

運行實例»##點擊"運行實例"按鈕查看在線實例

如需了解更多關於使用JavaScript 和XML DOM 的信息,請訪問我們的XML DOM 教學