?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
在下面的實(shí)例中,我們打開一個(gè) XML 文件("cd_catalog.xml"),然后遍歷每個(gè) 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>
Bob Dylan | Empire Burlesque |
Bonnie Tyler | Hide your heart |
Dolly Parton | Greatest Hits |
Gary Moore | Still got the blues |
Eros Ramazzotti | Eros |
Bee Gees | One night only |
Dr.Hook | Sylvias Mother |
Rod Stewart | Maggie May |
Andrea Bocelli | Romanza |
Percy Sledge | When a man loves a woman |
Savage Rose | Black angel |
Many | 1999 Grammy Nominees |
Kenny Rogers | For the good times |
Will Smith | Big Willie style |
Van Morrison | Tupelo Honey |
Jorn Hoel | Soulsville |
Cat Stevens | The very best of |
Sam Brown | Stop |
T'Pau | Bridge of Spies |
Tina Turner | Private Dancer |
Kim Larsen | Midt om natten |
Luciano Pavarotti | Pavarotti Gala Concert |
Otis Redding | The dock of the bay |
Simply Red | Picture book |
The Communards | Red |
Joe Cocker | Unchain my heart |
如需了解更多關(guān)于使用 JavaScript 和 XML DOM 的信息,請(qǐng)?jiān)L問我們的 XML DOM 教程。