首页 > 开发 > AJAX > 正文

AJAX教程(17):把XML文件显示为HTML表格

2024-09-01 08:26:07
字体:
来源:转载
供稿:网友
把xml文件显示为html表格。

<html>
<head>
<script type="text/javascript">
var xmlhttp;

function loadxmldoc(url)
{
xmlhttp=null;
if (window.xmlhttprequest)
  {// code for ie7, firefox, mozilla, etc.
  xmlhttp=new xmlhttprequest();
  }
else if (window.activexobject)
  {// code for ie5, ie6
  xmlhttp=new activexobject("microsoft.xmlhttp");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=onresponse;
  xmlhttp.open("get",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("your browser does not support xmlhttp.");
  }
}

function onresponse()
{
if(xmlhttp.readystate!=4) return;
if(xmlhttp.status!=200)
  {
  alert("problem retrieving xml data");
  return;
  }

txt="<table border='1'>";
x=xmlhttp.responsexml.documentelement.getelementsbytagname("cd");
for (i=0;i<x.length;i++)
  {
  txt=txt + "<tr>";
  xx=x[i].getelementsbytagname("title");
    {
    try
      {
      txt=txt + "<td>" + xx[0].firstchild.nodevalue + "</td>";
      }
    catch (er)
      {
      txt=txt + "<td> </td>";
      }
    }
  xx=x[i].getelementsbytagname("artist");
    {
    try
      {
      txt=txt + "<td>" + xx[0].firstchild.nodevalue + "</td>";
      }
    catch (er)
      {
      txt=txt + "<td> </td>";
      }
    }
  txt=txt + "</tr>";
  }
txt=txt + "</table>";
document.getelementbyid('copy').innerhtml=txt;
}

</script>
</head>

<body>
<div id="copy">
<button onclick="loadxmldoc('/example/xmle/cd_catalog.xml')">get cd info</button>
</div>
</body>
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表