分享

[xml]xml到html练习

 爪一o_0一斗 2013-05-28

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>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约