XML to HTML


在 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>
Bob DylanEmpire Burlesque
Bonnie TylerHide your heart
Dolly PartonGreatest Hits
Gary MooreStill got the blues
Eros RamazzottiEros
Bee GeesOne night only
Dr.HookSylvias Mother
Rod StewartMaggie May
Andrea BocelliRomanza
Percy SledgeWhen a man loves a woman
Savage RoseBlack angel
Many1999 Grammy Nominees
Kenny RogersFor the good times
Will SmithBig Willie style
Van MorrisonTupelo Honey
Jorn HoelSoulsville
Cat StevensThe very best of
Sam BrownStop
T'PauBridge of Spies
Tina TurnerPrivate Dancer
Kim LarsenMidt om natten
Luciano PavarottiPavarotti Gala Concert
Otis ReddingThe dock of the bay
Simply RedPicture book
The CommunardsRed
Joe CockerUnchain my heart

如需了解更多关于使用 JavaScript 和 XML DOM 的信息,请访问我们的 XML DOM 教程。