在 VBScript 中为 Dummy 导航 XML 节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9723903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:16:03  来源:igfitidea点击:

Navigating XML nodes in VBScript, for a Dummy

xmldomxpathvbscript

提问by user1167662

I am trying to write a script that will manipulate some data for me in an xml file. I am pretty new to VBScript but have a VB.NET and VBA background, so I feel like I kind of know what I am doing.

我正在尝试编写一个脚本,该脚本将在 xml 文件中为我处理一些数据。我是 VBScript 的新手,但有 VB.NET 和 VBA 背景,所以我觉得我有点知道我在做什么。

I thought there is probably a better way to navigate the file rather than a lot of calling InStr() or similar for each line to see if what I am looking for is there. My initial idea was to use a few methods I have seen in VB.NET from System.XML, since I had seen node navigating functions and members in that.

我认为可能有更好的方法来导航文件,而不是为每一行调用 InStr() 或类似方法来查看是否存在我要查找的内容。我最初的想法是使用一些我在 System.XML 中在 VB.NET 中看到的方法,因为我在其中看到了节点导航函数和成员。

After investigating this, I cannot find any way to import a namespace (System.XML, or otherwise) into VBScript without it running on a webpage. I decided to look for other options instead of spending more time searching for this.

对此进行调查后,我找不到任何方法将命名空间(System.XML 或其他)导入到 VBScript 中,而不会在网页上运行。我决定寻找其他选择,而不是花更多时间寻找这个。

Turns out there are other ways to do what I want, using methods and objects that specifically deal with navigating nodes of an XML file. I learned that some common examples of these "systems" (for lack of a better term, because I am sure that is improper) seem to be DOM and XPath.

原来还有其他方法可以做我想做的事,使用专门处理 XML 文件导航节点的方法和对象。我了解到这些“系统”的一些常见示例(由于缺乏更好的术语,因为我确信这是不合适的)似乎是 DOM 和 XPath。

I started by investigating XPath (since I had seen XPath deemed superior to DOM in a few places, such as: Traversing all nodes in an XML file with VBScript). I could not find anything to discribe the basics of XPath in vbscript. There is lots on the syntax for paths and such, but I could find nothing that describes the very basics of how to actually call that syntax in VBScript to make use of it. So I moved on to the next option.

我从研究 XPath 开始(因为我看到 XPath 在一些地方被认为优于 DOM,例如:使用 VBScript 遍历 XML 文件中的所有节点)。我在 vbscript 中找不到任何描述 XPath 基础的东西。关于路径等的语法有很多,但我找不到任何描述如何在 VBScript 中实际调用该语法以使用它的基础知识。所以我转向了下一个选项。

I then found many slightly different articles/questions/etc about DOM. So I gave it a try. Not a single one of them worked, all gave me errors. Mostly, it just seemed that a DOM object is never loaded correctly. Here are just a few of the methods I have tried for this:

然后我发现许多关于 DOM 的文章/问题/等略有不同。所以我试了一下。他们没有一个工作,都给了我错误。大多数情况下,似乎 DOM 对象从未正确加载。以下是我为此尝试过的几种方法:

From MSDN: a beginner's guide to XML DOM:

来自MSDN:XML DOM 初学者指南

Set objParser = CreateObject( "Microsoft.XMLDOM" )
Dim xDoc As MSXML.DOMDocument
Set xDoc = New MSXML.DOMDocument

If xDoc.Load("C:\My Documents\cds.xml") Then
   msgbox("Success!")
Else
   msgbox("Failure!")
End If

This returned failure every time.

每次都返回失败。

Based on another method:

基于另一种方法

dim xmlDom
set xmlDom = createobject("MSXML2.DOMDocument")
xmlDom.async = false
xmlDom.load ("C:\MyFileLocation\MyFile.xml")

and then I tried a few things to detect if it worked like message boxes for each node name in xmlDom.documentElement.

然后我尝试了一些方法来检测它是否像 xmlDom.documentElement 中每个节点名称的消息框一样工作。

I have tried so many other things I can't even remember most of them.

我尝试了很多其他的东西,我什至不记得其中的大部分。

I simply don't know what more I can try or why this isn't working for me. I am simply at a loss for what more I can try differently while still having syntax that COULD work.

我只是不知道我还能尝试什么,或者为什么这对我不起作用。我只是不知道我可以尝试什么不同的方法,同时仍然可以使用可以工作的语法。

So my question is: How can I navigate an XML file using VBScript without the script being imbedded in a webpage or otherwise? I need to know the extreme basics.

所以我的问题是:如何使用 VBScript 浏览 XML 文件而不将脚本嵌入网页或其他方式?我需要知道极端的基础知识。

I know my question likely seems stupid and ignorant since this should be easily available information, but I really cannot for the life of me find the basics I need to understand how to navigate nodes in ANY WAY using JUST VBScript (not in an html or asp file or something like that).

我知道我的问题可能看起来很愚蠢和无知,因为这应该是很容易获得的信息,但我真的无法在我的生活中找到我需要了解的基础知识,以了解如何仅使用 VBScript(不是在 html 或 asp 中)以任何方式导航节点文件或类似的东西)。

回答by

Here is a small example:

这是一个小例子:

Suppose you have a file called C:\Temp\Test.xmlwith this contents:

假设您有一个C:\Temp\Test.xml使用以下内容调用的文件:

<?xml version="1.0"?>
<root>
   <property name="alpha" value="1"/>
   <property name="beta" value="2"/>
   <property name="gamma" value="3"/>
</root>

Then you can use this VBScript:

然后你可以使用这个 VBScript:

Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load "C:\Temp\Test.xml"

' Iterate over all elements contained in the <root> element:

Set objRoot = objDoc.documentElement
s = ""
t = ""
For Each child in objRoot.childNodes
   s = s & child.getAttribute("name") & " "
   t = t & child.getAttribute("value") & " "
Next
MsgBox s    ' Displays "alpha beta gamma "
MsgBox t    ' Displays "1 2 3 "

' Find a particular element using XPath:

Set objNode = objDoc.selectSingleNode("/root/property[@name='beta']")
MsgBox objNode.getAttribute("value")     ' Displays 2

I hope this helps getting you started with VBScript and XML.

我希望这有助于您开始使用 VBScript 和 XML。