- XML DOM 基礎
- XML DOM - 首頁
- XML DOM - 概述
- XML DOM - 模型
- XML DOM - 節點
- XML DOM - 節點樹
- XML DOM - 方法
- XML DOM - 載入
- XML DOM - 遍歷
- XML DOM - 導航
- XML DOM - 訪問
- XML DOM 操作
- XML DOM - 獲取節點
- XML DOM - 設定節點
- XML DOM - 建立節點
- XML DOM - 新增節點
- XML DOM - 替換節點
- XML DOM - 刪除節點
- XML DOM - 克隆節點
- XML DOM 物件
- DOM - Node 物件
- DOM - NodeList 物件
- DOM - NamedNodeMap 物件
- DOM - DOMImplementation
- DOM - DocumentType 物件
- DOM - ProcessingInstruction
- DOM - Entity 物件
- DOM - EntityReference 物件
- DOM - Notation 物件
- DOM - Element 物件
- DOM - Attribute 物件
- DOM - CDATASection 物件
- DOM - Comment 物件
- DOM - XMLHttpRequest 物件
- DOM - DOMException 物件
- XML DOM 有用資源
- XML DOM - 快速指南
- XML DOM - 有用資源
- XML DOM - 討論
XML DOM - 遍歷
在本章中,我們將討論 XML DOM 遍歷。我們在上一章學習瞭如何載入 XML 文件並解析得到的 DOM 物件。這個解析後的 DOM 物件可以被遍歷。遍歷是一個按系統的方式迴圈遍歷節點樹中每個元素的過程,一步一步地進行。
示例
以下示例(traverse_example.htm)演示了 DOM 遍歷。這裡我們遍歷 <Employee> 元素的每個子節點。
<!DOCTYPE html>
<html>
<style>
table,th,td {
border:1px solid black;
border-collapse:collapse
}
</style>
<body>
<div id = "ajax_xml"></div>
<script>
//if browser supports XMLHttpRequest
if (window.XMLHttpRequest) {// Create an instance of XMLHttpRequest object.
code for IE7+, Firefox, Chrome, Opera, Safari
var xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// sets and sends the request for calling "node.xml"
xmlhttp.open("GET","/dom/node.xml",false);
xmlhttp.send();
// sets and returns the content as XML DOM
var xml_dom = xmlhttp.responseXML;
// this variable stores the code of the html table
var html_tab = '<table id = "id_tabel" align = "center">
<tr>
<th>Employee Category</th>
<th>FirstName</th>
<th>LastName</th>
<th>ContactNo</th>
<th>Email</th>
</tr>';
var arr_employees = xml_dom.getElementsByTagName("Employee");
// traverses the "arr_employees" array
for(var i = 0; i<arr_employees.length; i++) {
var employee_cat = arr_employees[i].getAttribute('category');
// gets the value of 'category' element of current "Element" tag
// gets the value of first child-node of 'FirstName'
// element of current "Employee" tag
var employee_firstName =
arr_employees[i].getElementsByTagName('FirstName')[0].childNodes[0].nodeValue;
// gets the value of first child-node of 'LastName'
// element of current "Employee" tag
var employee_lastName =
arr_employees[i].getElementsByTagName('LastName')[0].childNodes[0].nodeValue;
// gets the value of first child-node of 'ContactNo'
// element of current "Employee" tag
var employee_contactno =
arr_employees[i].getElementsByTagName('ContactNo')[0].childNodes[0].nodeValue;
// gets the value of first child-node of 'Email'
// element of current "Employee" tag
var employee_email =
arr_employees[i].getElementsByTagName('Email')[0].childNodes[0].nodeValue;
// adds the values in the html table
html_tab += '<tr>
<td>'+ employee_cat+ '</td>
<td>'+ employee_firstName+ '</td>
<td>'+ employee_lastName+ '</td>
<td>'+ employee_contactno+ '</td>
<td>'+ employee_email+ '</td>
</tr>';
}
html_tab += '</table>';
// adds the html table in a html tag, with id = "ajax_xml"
document.getElementById('ajax_xml').innerHTML = html_tab;
</script>
</body>
</html>
此程式碼載入 node.xml。
XML 內容被轉換為 JavaScript XML DOM 物件。
使用 getElementsByTagName() 方法獲取元素(帶有 Element 標籤)的陣列。
接下來,我們遍歷此陣列並在表格中顯示子節點值。
執行
將此檔案儲存為 traverse_example.html 到伺服器路徑(此檔案和 node.xml 應位於伺服器上的同一路徑)。您將收到以下輸出:
廣告