• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

JavaScript将xml转换成html table

JavaScript 水墨上仙 1648次浏览

JavaScript将xml转换成html table

function ConvertToTable(targetNode)
{
    // if the targetNode is xmlNode this line must be removed
    // i couldnt find a way to parse xml string to xml node
    // so i parse xml string to xml document
    targetNode = targetNode.childNodes[0];
    // first we need to create headers
    var columnCount = targetNode.childNodes[0].childNodes.length;
    var rowCount = targetNode.childNodes.length
    // name for the table
    var myTable = document.createElement("table");
    myTable.border = 1;
    myTable.borderColor ="green";
    var firstRow = myTable.insertRow();
    var firstCell = firstRow.insertCell();
    firstCell.colSpan = columnCount;
    firstCell.innerHTML = targetNode.nodeName;
    // name for the columns
    var secondRow = myTable.insertRow();
    for(var i=0;i<columnCount;i++)
    {
        var newCell = secondRow.insertCell();
        newCell.innerHTML = targetNode.childNodes[0].childNodes[i].nodeName;
    }
    // now fill the rows with data
    for(var i2=0;i2<rowCount;i2++)
    {
        var newRow = myTable.insertRow();
         for(var j=0;j<columnCount;j++)
         {
            var newCell = newRow.insertCell();
            newCell.innerHTML = targetNode.childNodes[i2].childNodes[j].firstChild.nodeValue;
         }
    }
    // i prefer to send it as string instead of a table object
    return myTable.outerHTML;
}
// example usage on a web page starts below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    
</head>
<body>
<script type="text/javascript">
function ConvertToTable(targetNode)
{
    // if the targetNode is xmlNode this line must be removed
    // i couldnt find a way to parse xml string to xml node
    // so i parse xml string to xml document
    targetNode = targetNode.childNodes[0];
    // first we need to create headers
    var columnCount = targetNode.childNodes[0].childNodes.length;
    var rowCount = targetNode.childNodes.length
    // name for the table
    var myTable = document.createElement("table");
    myTable.border = 1;
    myTable.borderColor ="green";
    var firstRow = myTable.insertRow();
    var firstCell = firstRow.insertCell();
    firstCell.colSpan = columnCount;
    firstCell.innerHTML = targetNode.nodeName;
    // name for the columns
    var secondRow = myTable.insertRow();
    for(var i=0;i<columnCount;i++)
    {
        var newCell = secondRow.insertCell();
        newCell.innerHTML = targetNode.childNodes[0].childNodes[i].nodeName;
    }
    // now fill the rows with data
    for(var i2=0;i2<rowCount;i2++)
    {
        var newRow = myTable.insertRow();
         for(var j=0;j<columnCount;j++)
         {
            var newCell = newRow.insertCell();
            newCell.innerHTML = targetNode.childNodes[i2].childNodes[j].firstChild.nodeValue;
         }
    }
    // i prefer to send it as string instead of a table object
    return myTable.outerHTML;
}
function loadXmlDocFromString(text)
{
    try //Internet Explorer
      {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(text);
      return xmlDoc;
      }  
    catch(e)
      {
      try // Firefox, Mozilla, Opera, etc.
        {
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(text,"text/xml");
        return xmlDoc;
        }
      catch(e)
        {
        alert(e.message);
        return;
        }
      }
}
var myXml = '<TableName> \
      <firstRow> \
        <field1>1</field1> \
        <field2>2</field2> \
      </firstRow> \
      <firstRow> \
        <field1>3</field1> \
        <field2>4</field2> \
      </firstRow> \
    </TableName>';
 var myDoc = loadXmlDocFromString(myXml);
document.write( ConvertToTable(myDoc));
</script>
</body>
</html>


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明JavaScript将xml转换成html table
喜欢 (0)
加载中……