VBScript MSXML2.IXMLDOMNodeList 对象
最后修改于 2025 年 4 月 9 日
VBScript 中的 MSXML2.IXMLDOMNodeList
对象代表 XML 节点的集合。它是 Microsoft XML Core Services (MSXML) 库的一部分。该对象提供了访问和操作 XML 文档中节点的方法。它通常由 getElementsByTagName
等方法返回。
IXMLDOMNodeList
允许遍历节点并通过索引访问它们。它维护节点在 XML 文档中出现的顺序。本教程将通过实际示例介绍 IXMLDOMNodeList
,以演示其在 VBScript 中的用法。
IXMLDOMNodeList 对象概述
IXMLDOMNodeList
接口提供了几个关键属性和方法。length
属性返回集合中节点的数量。item
方法通过索引位置检索节点。
集合中的节点是从零开始索引的。该对象是活动的,这意味着对 XML 文档的更改会自动反映出来。理解此对象对于 VBScript 应用程序中的有效 XML 处理至关重要。
基本 NodeList 遍历
此示例演示了如何加载 XML 文档并遍历 NodeList 中的所有节点。我们将使用 getElementsByTagName
方法检索所有具有特定标签名的元素。
Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0") xmlDoc.async = False xmlDoc.load("books.xml") If xmlDoc.parseError.errorCode <> 0 Then WScript.Echo "Error loading XML: " & xmlDoc.parseError.reason WScript.Quit End If Set bookNodes = xmlDoc.getElementsByTagName("book") For i = 0 To bookNodes.length - 1 WScript.Echo "Book Title: " & bookNodes.item(i).getAttribute("title") Next Set bookNodes = Nothing Set xmlDoc = Nothing
脚本加载 XML 文件并检索所有 book
元素。然后,它使用 For 循环遍历 NodeList。对于每个节点,它会输出 title
属性的值。length
属性决定了循环的界限。
访问 NodeList 中的特定节点
此示例演示了如何通过索引访问 NodeList 中的特定节点。我们将同时演示直接访问和在访问前检查节点是否存在。当您需要特定节点而不是遍历所有节点时,这将非常有用。
Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0") xmlDoc.async = False xmlDoc.load("employees.xml") If xmlDoc.parseError.errorCode <> 0 Then WScript.Echo "Error loading XML: " & xmlDoc.parseError.reason WScript.Quit End If Set empNodes = xmlDoc.getElementsByTagName("employee") ' Access first employee If empNodes.length > 0 Then WScript.Echo "First employee: " & empNodes.item(0).text End If ' Access third employee if exists If empNodes.length >= 3 Then WScript.Echo "Third employee: " & empNodes.item(2).text Else WScript.Echo "Third employee not found" End If Set empNodes = Nothing Set xmlDoc = Nothing
脚本在访问特定索引之前会检查 NodeList 的长度。这可以防止尝试访问不存在的节点时出错。item
方法通过零基位置检索节点。在访问特定索引之前,请务必验证 NodeList 的长度。
修改 NodeList 中的节点
此示例演示了如何在 NodeList 中修改节点。由于 NodeList 是活动的,对节点的更改会立即反映在 XML 文档中。我们将更新所有匹配节点的属性值。
Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0") xmlDoc.async = False xmlDoc.load("products.xml") If xmlDoc.parseError.errorCode <> 0 Then WScript.Echo "Error loading XML: " & xmlDoc.parseError.reason WScript.Quit End If Set productNodes = xmlDoc.getElementsByTagName("product") For i = 0 To productNodes.length - 1 productNodes.item(i).setAttribute "status", "active" Next ' Save modified XML xmlDoc.save "products_updated.xml" Set productNodes = Nothing Set xmlDoc = Nothing
脚本加载 XML 文件并检索所有 product
元素。然后,它遍历 NodeList 并更新每个产品的 status
属性。更改会立即反映在 NodeList 中并保存到新文件中。这演示了 NodeList 集合的活动性质。
过滤 NodeList 中的节点
虽然 NodeList 没有内置的过滤功能,但我们可以实现自定义过滤逻辑。此示例展示了如何仅处理满足特定标准的节点。我们将在遍历时检查属性值。
Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0") xmlDoc.async = False xmlDoc.load("orders.xml") If xmlDoc.parseError.errorCode <> 0 Then WScript.Echo "Error loading XML: " & xmlDoc.parseError.reason WScript.Quit End If Set orderNodes = xmlDoc.getElementsByTagName("order") WScript.Echo "Processing high-value orders:" For i = 0 To orderNodes.length - 1 Set order = orderNodes.item(i) amount = CDbl(order.getAttribute("amount")) If amount > 1000 Then WScript.Echo "Order ID: " & order.getAttribute("id") & _ ", Amount: $" & amount End If Next Set orderNodes = Nothing Set xmlDoc = Nothing
脚本仅处理金额大于 1000 美元的订单。它演示了在使用 NodeList 时如何实现自定义过滤逻辑。在遍历过程中会检查每个节点的属性,以确定是否应该进行处理。这种模式对于选择性节点处理很有用。
将 NodeList 转换为数组
有时将 NodeList 转换为数组以方便处理会很有用。此示例展示了如何从 NodeList 创建数组。我们将保留数组中的节点引用,以便后续处理。
Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0") xmlDoc.async = False xmlDoc.load("customers.xml") If xmlDoc.parseError.errorCode <> 0 Then WScript.Echo "Error loading XML: " & xmlDoc.parseError.reason WScript.Quit End If Set customerNodes = xmlDoc.getElementsByTagName("customer") ' Create array to hold nodes Dim customerArray() ReDim customerArray(customerNodes.length - 1) ' Populate array For i = 0 To customerNodes.length - 1 Set customerArray(i) = customerNodes.item(i) Next ' Process array For Each cust In customerArray WScript.Echo "Customer: " & cust.getAttribute("name") Next Set customerNodes = Nothing Set xmlDoc = Nothing
脚本创建一个大小与 NodeList 长度匹配的数组。然后,它将每个节点引用复制到数组中。该数组可以独立于原始 NodeList 进行处理。当您需要使用静态节点集合时,此技术很有用。
来源
在本文中,我们探讨了 VBScript 中的 MSXML2.IXMLDOMNodeList
对象,涵盖了其用法和实际应用。从基本遍历到高级过滤和转换技术,这些示例演示了有效的 XML 处理。通过这些知识,您可以增强 VBScript 应用程序的健壮 XML 处理能力。