DragonWar% 2015-03-02 22:57 采纳率: 0%
浏览 51

将XML文件解析为表格

This is the XML file that needs to be parsed, we will call it "servers.xml". This file is on the same server I want it parsed (xml file in same folder).

<root>
    <list>
        <server>
            <server name="28 Disconnects Later">
                <timestamp name="2015-02-25 14:28:56">low</timestamp>
                <timestamp name="2015-02-25 14:58:56">low</timestamp>
                <timestamp name="2015-02-25 15:28:57">low</timestamp>
                <timestamp name="2015-02-25 15:58:58">low</timestamp>
                <timestamp name="2015-02-25 16:28:59">low</timestamp>
                <timestamp name="2015-02-25 16:59:00">low</timestamp>
                <timestamp name="2015-02-25 17:29:01">low</timestamp>
                <timestamp name="2015-02-25 17:59:02">low</timestamp>
                <timestamp name="2015-02-25 18:29:04">low</timestamp>
                <timestamp name="2015-02-25 18:59:05">low</timestamp>
            </server>
            <server name="Abomination">
                <timestamp name="2015-02-25 14:28:56">high</timestamp>
                <timestamp name="2015-02-25 14:58:56">high</timestamp>
                <timestamp name="2015-02-25 15:28:57">high</timestamp>
                <timestamp name="2015-02-25 15:58:58">high</timestamp>
                <timestamp name="2015-02-25 16:28:59">high</timestamp>
                <timestamp name="2015-02-25 16:59:00">high</timestamp>
                <timestamp name="2015-02-25 17:29:01">high</timestamp>
                <timestamp name="2015-02-25 17:59:02">high</timestamp>
                <timestamp name="2015-02-25 18:29:04">high</timestamp>
                <timestamp name="2015-02-25 18:59:05">high</timestamp>
            </server>
        </server>
    </list>
</root>

I need to sort it into a table like so:

|----------------------------------|
| server name                      |
|----------------------------------|
| timestamp name | timestamp value |
| timestamp name | timestamp value |
| timestamp name | timestamp value |
| timestamp name | timestamp value |
| timestamp name | timestamp value |
|----------------------------------|
|                                  |
|----------------------------------|
| server name                      |
|----------------------------------|
| timestamp name | timestamp value |
| timestamp name | timestamp value |
| timestamp name | timestamp value |
| timestamp name | timestamp value |
| timestamp name | timestamp value |

I have been working on how todo this for a couple of hours but can't seem to get stuff to display correctly or at all.

Any help is appreciated

EDIT CURRENT CODE:

<!DOCTYPE html>
<html>
<head>
    <style>
        table, th, td {
            border: 1px solid black;
            border-collapse:collapse;
        }
        th, td {
            padding: 5px;
        }
    </style>
</head>
<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","AOD-H1Z1.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;

    document.write("<table><tr><th colspan='2'>Server</th></tr>");
    var x=xmlDoc.getElementsByTagName("server");
    for (i=0;i<x.length;i++)
    {
        document.write("<tr><td>");
        document.write(x[i].getElementsByTagName("timestamp")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("timestamp")[0].childNodes[0].nodeValue);
        document.write("</td></tr>");
    }
    document.write("</table>");
</script>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • weixin_33698043 2015-03-02 23:37
    关注

    First off, I noticed you had <server> nodes nested inside other <server> nodes. A preferable syntax would be to remove those wrapping nodes, or rename them, to <servers>, for example (that's what I assumed in the following code).

    Edit: Since your xml is already parsed from the Ajax request, you can skip the xmlString, xmlDoc and parsing part.

    You can parse it this way:

    var xmlString = '<root><list><server><server name="28 Disconnects Later"><timestamp name="2015-02-25 14:28:56">low</timestamp><timestamp name="2015-02-25 14:58:56">low</timestamp><timestamp name="2015-02-25 15:28:57">low</timestamp><timestamp name="2015-02-25 15:58:58">low</timestamp><timestamp name="2015-02-25 16:28:59">low</timestamp><timestamp name="2015-02-25 16:59:00">low</timestamp><timestamp name="2015-02-25 17:29:01">low</timestamp><timestamp name="2015-02-25 17:59:02">low</timestamp><timestamp name="2015-02-25 18:29:04">low</timestamp><timestamp name="2015-02-25 18:59:05">low</timestamp></server><server name="Abomination"><timestamp name="2015-02-25 14:28:56">high</timestamp><timestamp name="2015-02-25 14:58:56">high</timestamp><timestamp name="2015-02-25 15:28:57">high</timestamp><timestamp name="2015-02-25 15:58:58">high</timestamp><timestamp name="2015-02-25 16:28:59">high</timestamp><timestamp name="2015-02-25 16:59:00">high</timestamp><timestamp name="2015-02-25 17:29:01">high</timestamp><timestamp name="2015-02-25 17:59:02">high</timestamp><timestamp name="2015-02-25 18:29:04">high</timestamp><timestamp name="2015-02-25 18:59:05">high</timestamp></server></server></list></root>',
        xmlDoc,
        // Create your table element
        table = document.createElement('table');
    
    // Parse the xml
    if (window.DOMParser){ // Standard browsers
        var parser = new DOMParser();
        xmlDoc = parser.parseFromString(xmlString, "text/xml");
    }
    else { // Internet Explorer
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(xmlString); 
    }
    
    var servers = xmlDoc.getElementsByTagName('list')[0].childNodes[0]
    .getElementsByTagName('server');
    // for each server
    for(var i=0, l=servers.length; i<l; i++){
        var server = servers[i];
        // Insert a row
        var tr = table.insertRow();
        // Insert a cell
        var td = tr.insertCell();
        // Make it spread over 2 columns
        td.colSpan = '2';
        // Insert the server name
        td.innerHTML = server.getAttribute('name');
    
        var timestamps = server.getElementsByTagName('timestamp');
        // For each timestamp
        for(var j=0, k=timestamps.length; j<k; j++){
            var timestamp = timestamps[j];
            // Insert a row
            tr = table.insertRow();
            // Insert a cell
            td = tr.insertCell();
            // Insert the timestamp name
            td.innerHTML = timestamp.getAttribute('name');
            // Insert a cell
            td = tr.insertCell();
            // Insert the timestamp value
            td.innerHTML = timestamp.innerHTML;
        }
    }
    
    // Append it to the body?
    document.body.appendChild(table);
    * {
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    table {
      margin: 1em auto;
      border: 1px solid black;
      border-collapse: collapse;
      font-family: Arial, Helvetica, sans-serif;
    }
    td {
      padding: .5em .8em;
      border: 1px solid #000;
      text-align: center;
    }
    td[colspan='2'] {
      background: #333;
      color: #fff;
    }

    JS Fiddle Demo

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂