打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[xml]xml到html练习

1.加载后,以表格形式输出数据:
<html>
<body>

<script type="text/javascript">
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","/example/xdom/books.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("book");

 document.write("<tr><th>category</th><th>title</th><th>author</th></tr>");
for (i=0;i<x.length;i++)
  {
  document.write("<tr><td>");
document.write(x[i].getAttribute("category"));  
  document.write("</td><td>");
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue);
  document.write("</td></tr>");
  }
document.write("</table>");
</script>

</body>
</html>

2.导航查看数据:
<html>
<head>

<script type="text/javascript">
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","/example/xmle/cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 


x=xmlDoc.getElementsByTagName("CD");
i=0;

function displayCD()
{
art= x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue ;
tit = x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue ;
year= x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue ;
txt="ARTIST:"+art+"<br/>TITLE:"+tit+"<br/>YEAR:"+year;

document.getElementById("show").innerHTML=txt;
}

function next()
{
if(i<x.length-1)
{
i++;
displayCD();
}
}

function previous()
{
if(i>0)
{
i--;
displayCD();
}
}
</script>
</head>
<body onload="displayCD()">

    <div id='show'></div></br>
    <input type="button" onclick="previous()"   value="<<"/>
    <input type="button" onclick="next()"   value=">>"/>

</body>
</html>

3.当点击专辑信息时,出现专辑详情:
<html>
<head>
<script type="text/javascript">
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","/example/xmle/cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 
x=xmlDoc.getElementsByTagName("CD");

function displayCDInfo(i)
{
artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
country=(x[i].getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue);
company=(x[i].getElementsByTagName("COMPANY")[0].childNodes[0].nodeValue);
price=(x[i].getElementsByTagName("PRICE")[0].childNodes[0].nodeValue);
txt="Artist: "+artist+"<br />Title: "+title+"<br />Year: "+year+"<br />Country: "+country+"<br />Company: "+company+"<br />Price: "+price  ;
document.getElementById("showCD").innerHTML=txt;
}
</script>
</head>

<body>
<div id='showCD'>点击某个 CD 就可显示专辑信息:</div><br />
<script type="text/javascript">
document.write("<table border='1'><tr><th>artist</th><th>title</th></tr>");
 for(var i=0;i<x.length;i++){
    document.write("<tr onclick='displayCDInfo("+i+")'><td>");
    document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue+"</td><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue+"</td></tr>");

}
document.write("</table>");
</script>

</body>
</html>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
XML DOM
我的XML学习笔记
使用js解析xml文档和xml字符串(ie和火狐)
XML DOM - Get Nodes
struct加Ajax
(2)vb 读取 XML 文件 内容
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服