<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="children"> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="cooking"> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="web"> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> <book category="web"> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> </bookstore> 按照上述xml文件,使用html和javascript读取.xml文件的节点内容,以表格方式对内容进行显示,通过下拉单进行交互式书目内容选取,仅显示选中的书目信息
时间: 2023-07-02 16:05:47 浏览: 103
XML应用开发(软件品牌)-1期 6.1 案例分析-DOM树结构.doc
以下是一个简单的实现方式:
HTML文件:
```html
<!DOCTYPE html>
<html>
<head>
<title>Interactive Bookstore</title>
<script src="script.js"></script>
</head>
<body>
<h1>Interactive Bookstore</h1>
<label for="books">Select a book:</label>
<select id="books" onchange="displayBookInfo()">
<option value="">--Select a book--</option>
</select>
<br><br>
<table id="bookInfoTable">
<tr>
<th>Category</th>
<th>Author(s)</th>
<th>Year</th>
<th>Price</th>
</tr>
</table>
</body>
</html>
```
JavaScript文件:
```javascript
// 定义一个变量来保存XML文件
var xmlDoc;
// 当页面加载时读取XML文件
window.onload = function() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
xmlDoc = this.responseXML;
populateBookList();
}
};
xmlhttp.open("GET", "books.xml", true);
xmlhttp.send();
}
// 填充下拉单的选项
function populateBookList() {
var selectNode = document.getElementById("books");
// 获取所有书本的节点
var bookNodes = xmlDoc.getElementsByTagName("book");
// 遍历所有书本的节点,并将它们添加到下拉单选项中
for (var i = 0; i < bookNodes.length; i++) {
var optionNode = document.createElement("option");
optionNode.textContent = bookNodes[i].getElementsByTagName("author")[0].textContent + " - " + bookNodes[i].getElementsByTagName("year")[0].textContent;
optionNode.value = i;
selectNode.appendChild(optionNode);
}
}
// 显示选中书本的信息
function displayBookInfo() {
var selectNode = document.getElementById("books");
var index = selectNode.value;
var bookNodes = xmlDoc.getElementsByTagName("book");
var bookNode = bookNodes[index];
// 清空表格内容
var tableNode = document.getElementById("bookInfoTable");
tableNode.innerHTML = "";
tableNode.appendChild(createTableRow("Category", bookNode.getAttribute("category")));
tableNode.appendChild(createTableRow("Author(s)", getAuthors(bookNode)));
tableNode.appendChild(createTableRow("Year", bookNode.getElementsByTagName("year")[0].textContent));
tableNode.appendChild(createTableRow("Price", bookNode.getElementsByTagName("price")[0].textContent));
}
// 获取书本的所有作者
function getAuthors(bookNode) {
var authorNodes = bookNode.getElementsByTagName("author");
var authorList = "";
for (var i = 0; i < authorNodes.length; i++) {
if (i > 0) {
authorList += ", ";
}
authorList += authorNodes[i].textContent;
}
return authorList;
}
// 创建一个表格行
function createTableRow(label, value) {
var trNode = document.createElement("tr");
var tdLabelNode = document.createElement("td");
tdLabelNode.textContent = label;
var tdValueNode = document.createElement("td");
tdValueNode.textContent = value;
trNode.appendChild(tdLabelNode);
trNode.appendChild(tdValueNode);
return trNode;
}
```
注意,这个例子假设XML文件名为“books.xml”,需要放在同一目录下。可以根据需要进行修改。
阅读全文