XML DOM(文档对象模型)为开发者提供了许多属性和方法,用于操作 XML 文档的结构、元素、属性等内容。通过这些属性和方法,开发者可以方便地读取、修改、删除 XML 文档中的数据。
在 XML DOM 中,属性用于访问和修改节点的基本信息,而方法则用于操作和查询节点内容、结构和关系。以下是 XML DOM 中常见的属性和方法。
1. 常见属性
这些属性用于访问或设置节点的基本信息,如节点的类型、名称、值等。
a) nodeName
返回节点的名称。对于元素节点,它是元素的标签名;对于其他类型的节点,可能会有所不同。
示例代码:
var node = xmlDoc.getElementsByTagName("title")[0];
console.log(node.nodeName); // 输出: title
b) nodeValue
返回或设置节点的值。对于文本节点,它包含节点的文本内容;对于元素节点或其他节点类型,它通常返回 null
。
示例代码:
var textNode = xmlDoc.getElementsByTagName("title")[0].firstChild;
console.log(textNode.nodeValue); // 输出: Learning XML
c) nodeType
返回节点的类型。常见的节点类型有:
1
:元素节点3
:文本节点7
:处理指令节点8
:注释节点
示例代码:
var node = xmlDoc.getElementsByTagName("title")[0];
console.log(node.nodeType); // 输出: 1 (元素节点)
d) parentNode
返回当前节点的父节点。如果当前节点是根节点或没有父节点,返回 null
。
示例代码:
var node = xmlDoc.getElementsByTagName("title")[0];
console.log(node.parentNode.nodeName); // 输出: book
e) childNodes
返回一个包含当前节点所有子节点的 NodeList 对象。该属性是一个集合,可以通过索引访问每个子节点。
示例代码:
var node = xmlDoc.getElementsByTagName("bookstore")[0];
console.log(node.childNodes.length); // 输出子节点的数量
2. 常见方法
这些方法用于操作和查询 XML 文档中的节点内容、属性和结构。
a) getElementsByTagName()
返回一个包含指定标签名的所有元素节点的 NodeList
对象。该方法是区分大小写的。
示例代码:
var titles = xmlDoc.getElementsByTagName("title");
console.log(titles[0].nodeValue); // 输出: Learning XML
b) getAttribute()
返回指定元素的属性值。如果元素没有该属性,返回 null
。
示例代码:
var book = xmlDoc.getElementsByTagName("book")[0];
console.log(book.getAttribute("lang")); // 输出: en
c) setAttribute()
设置指定元素的属性值。如果属性不存在,它将创建该属性。
示例代码:
var book = xmlDoc.getElementsByTagName("book")[0];
book.setAttribute("lang", "fr"); // 将属性 lang 的值设置为 "fr"
console.log(book.getAttribute("lang")); // 输出: fr
d) createElement()
创建一个新的元素节点,但尚未将其添加到文档中。
示例代码:
var newElement = xmlDoc.createElement("price");
newElement.textContent = "29.99";
console.log(newElement.nodeName); // 输出: price
e) createTextNode()
创建一个新的文本节点。
示例代码:
var newTextNode = xmlDoc.createTextNode("New Book Title");
console.log(newTextNode.nodeValue); // 输出: New Book Title
f) appendChild()
将一个节点添加到当前节点的子节点列表的末尾。
示例代码:
var newElement = xmlDoc.createElement("price");
newElement.textContent = "29.99";
var book = xmlDoc.getElementsByTagName("book")[0];
book.appendChild(newElement);
g) removeChild()
从父节点中删除指定的子节点。
示例代码:
var book = xmlDoc.getElementsByTagName("book")[0];
var title = book.getElementsByTagName("title")[0];
book.removeChild(title); // 删除 <title> 元素
h) replaceChild()
用新的节点替换指定的子节点。
示例代码:
var newElement = xmlDoc.createElement("price");
newElement.textContent = "29.99";
var book = xmlDoc.getElementsByTagName("book")[0];
var oldElement = book.getElementsByTagName("title")[0];
book.replaceChild(newElement, oldElement); // 替换 <title> 节点
i) normalize()
将节点的子节点合并成一个单一的文本节点,去除多余的空白和空文本节点。
示例代码:
var book = xmlDoc.getElementsByTagName("book")[0];
book.normalize(); // 合并文本节点
3. 其他常见方法
a) cloneNode()
创建当前节点的副本,可以选择是否复制其子节点。
示例代码:
var book = xmlDoc.getElementsByTagName("book")[0];
var clonedBook = book.cloneNode(true); // 深复制,复制所有子节点
console.log(clonedBook.nodeName); // 输出: book
b) hasChildNodes()
检查当前节点是否有子节点。返回布尔值。
示例代码:
var bookstore = xmlDoc.getElementsByTagName("bookstore")[0];
console.log(bookstore.hasChildNodes()); // 输出: true 或 false
总结
XML DOM 提供了丰富的属性和方法,可以帮助开发者有效地操作和管理 XML 文档。常见的属性包括 nodeName
、nodeValue
、parentNode
和 childNodes
等,用于访问节点的基本信息。常见的方法,如 getElementsByTagName()
、setAttribute()
、createElement()
、appendChild()
等,用于操作节点的内容、属性和结构。
这些属性和方法结合使用,可以实现对 XML 文档的高效解析、修改和操作。
发表回复