XML綁定到Table

來源:互聯網
上載者:User

XML描述的資料是可以綁定到表格上的.下面是一個例子:

HTML代碼如下:


<html>
<script>...
function test()
...{
    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.load("cd_catalog.xml");
    var nodes= xmlDoc.documentElement.selectNodes("CD/TITLE");//擷取所有匹配的(TITLE)節點的值
    for(var i=0;i<nodes.length;i++)
    ...{
        alert(nodes[i].text+" ");
    }
    //alert(xmlDoc.xml);
    //var node=xmlDoc.documentElement.selectSingleNode("CD/TITLE");//擷取匹配的第一個(TITLE)節點的值
    //alert(node.text);
}


</script>
<body onLoad="test()">

<xml id="cdcat" src="cd_catalog.xml" tppabs="http://www.w3schools.com/xml/cd_catalog.xml"></xml>
<br>
<table border="1" datasrc="#cdcat">
<thead>
<th>ARTIST</th>
<th>TITLE</th>
</thead>
<tr>
<td>
<span datafld="ARTIST"></span>
</td>
<td>
<span datafld="TITLE"></span>
</td>
</tr>
</table>
</body>
</html>

 用到的XML檔案請參見我寫的文章:javascript實現XML導航。

註:在table標籤上用datasrc=“#xmlID”來綁定XML檔案,xmlID是xml標籤的id屬性。datafld屬性只能寫在span或是div中(好像是這樣的,讀者可以嘗試寫在其他標籤中),格式為datafld=“name”,其中name是要綁定到該span或是div的值在xml檔案中的描述(必須和XML檔案中某個標籤一致)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.