VBScript XML DOM (MSXML)
最后修改于 2025 年 4 月 4 日
VBScript 可以使用 Microsoft XML Core Services (MSXML) 来处理 XML 文档。本教程涵盖了 MSXML 中可用的 XML DOM 对象和方法。您将通过实际示例学习如何在 VBScript 中解析、创建和操作 XML 文档。
XML DOM 概述
XML 文档对象模型 (DOM) 为 XML 文档提供了编程接口。MSXML 是 Microsoft 对 XML DOM 的实现。它允许您以编程方式加载、解析、修改和保存 XML 文档。
MSXML 中的关键对象包括 DOMDocument
、IXMLDOMNode
、IXMLDOMNodeList
和 IXMLDOMElement
。这些对象提供了导航和操作 XML 文档的方法。我们将在后续章节中通过示例逐一探讨它们。
加载和解析 XML 文件
此示例演示了如何使用 MSXML 加载和解析 XML 文件。DOMDocument
对象用于加载 XML 文件。然后,我们遍历文档以显示其内容。
Dim xmlDoc, root, books, book 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 root = xmlDoc.documentElement Set books = root.getElementsByTagName("book") For Each book In books WScript.Echo "Title: " & book.getAttribute("title") WScript.Echo "Author: " & book.getAttribute("author") Next
此脚本加载一个名为 books.xml 的 XML 文件。它使用 parseError 对象检查解析错误。然后,它检索所有 book 元素并显示它们的 title 和 author 属性。async 属性设置为 false 以实现同步加载。
创建新的 XML 文档
此示例展示了如何从头开始创建新的 XML 文档。我们将使用 DOMDocument
对象及其方法来创建元素和属性。生成的 XML 将被保存到文件中。
Dim xmlDoc, root, employee, name, position Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0") ' Create processing instruction and root element xmlDoc.appendChild xmlDoc.createProcessingInstruction("xml", "version='1.0'") Set root = xmlDoc.createElement("employees") xmlDoc.appendChild root ' Create employee element Set employee = xmlDoc.createElement("employee") root.appendChild employee ' Add attributes to employee employee.setAttribute "id", "1001" ' Create child elements Set name = xmlDoc.createElement("name") name.text = "John Smith" employee.appendChild name Set position = xmlDoc.createElement("position") position.text = "Software Developer" employee.appendChild position ' Save to file xmlDoc.save "employees.xml" WScript.Echo "XML document created successfully"
此脚本创建一个包含员工数据的新 XML 文档。它从 XML 声明开始,创建一个根元素,并添加具有文本内容的子元素。最后,它将文档保存到文件中。每一步都演示了用于构建 XML 结构的各种 DOM 方法。
修改现有的 XML 文档
此示例演示了如何修改现有的 XML 文档。我们将加载一个 XML 文件,更改其内容,然后保存修改后的版本。该示例展示了添加、更新和删除节点。
Dim xmlDoc, root, books, newBook 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 root = xmlDoc.documentElement Set books = root.getElementsByTagName("book") ' Update first book's title If books.length > 0 Then books(0).setAttribute "title", "Updated Title" End If ' Add a new book Set newBook = xmlDoc.createElement("book") newBook.setAttribute "title", "New Book" newBook.setAttribute "author", "New Author" root.appendChild newBook ' Remove the last book If books.length > 1 Then root.removeChild books(books.length - 1) End If ' Save changes xmlDoc.save "books_modified.xml" WScript.Echo "XML document modified successfully"
此脚本加载一个 XML 文件,更新现有图书的标题,添加一本新图书,并删除最后一本图书。它演示了使用 DOM 方法进行的常见修改操作。然后将更改保存到新文件中,保留原始文件。
使用 XPath 搜索 XML
XPath 是一种强大的查询语言,用于在 XML 文档中选择节点。此示例展示了如何在 MSXML 中使用 XPath 表达式查找特定节点。我们将搜索特定作者的书籍。
Dim xmlDoc, nodeList, node 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 selection language to XPath xmlDoc.setProperty "SelectionLanguage", "XPath" ' Find books by specific author Set nodeList = xmlDoc.selectNodes("//book[@author='J.R.R. Tolkien']") If nodeList.length > 0 Then WScript.Echo "Found " & nodeList.length & " books by J.R.R. Tolkien:" For Each node In nodeList WScript.Echo "- " & node.getAttribute("title") Next Else WScript.Echo "No books found by J.R.R. Tolkien" End If
此脚本使用 XPath 搜索特定作者的书籍。selectNodes
方法执行 XPath 查询,返回一个节点列表。然后我们遍历结果。XPath 提供了比简单元素名称搜索更灵活的查询功能。
处理 XML 命名空间
XML 命名空间可防止复杂文档中的元素名称冲突。此示例演示了如何在 VBScript 中使用带命名空间的 XML。我们将加载一个包含命名空间的文档并访问其元素。
Dim xmlDoc, root, ns, products, product 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 up namespace manager Set ns = CreateObject("MSXML2.IXMLDOMSchemaCollection") xmlDoc.setProperty "SelectionNamespaces", "xmlns:p='http://example.com/products'" ' Access elements with namespace prefix Set root = xmlDoc.documentElement Set products = xmlDoc.selectNodes("//p:product") WScript.Echo "Found " & products.length & " products:" For Each product In products WScript.Echo "ID: " & product.getAttribute("id") & _ ", Name: " & product.selectSingleNode("p:name").text Next
此脚本处理一个包含命名空间的 XML 文档。我们使用 SelectionNamespaces
属性来声明命名空间前缀。然后,XPath 查询使用这些前缀来访问带命名空间的元素。这种方法对于处理 SOAP 或 XHTML 等标准化 XML 格式至关重要。
来源
在本文中,我们使用 MSXML 探讨了在 VBScript 中处理 XML 的基础知识。从加载和解析到创建和修改 XML 文档,这些示例提供了坚实的基础。通过这些技术,您可以有效地将 XML 处理集成到您的 VBScript 自动化任务中。