在 VBScript 中导航 XML 节点,用于 Dummy [英] Navigating XML nodes in VBScript, for a Dummy

查看:15
本文介绍了在 VBScript 中导航 XML 节点,用于 Dummy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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.

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

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.

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

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.

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

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:

来自 MSDN:XML DOM 初学者指南:

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

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

每次都返回失败.

基于另一种方法:

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

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

and then I tried a few things to detect if it worked like message boxes for each node name in 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.

所以我的问题是:如何在不将脚本嵌入网页或其他方式的情况下使用 VBScript 导航 XML 文件?我需要了解极端的基础知识.

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 以任何方式导航节点(而不是在html 或 asp 文件或类似的文件).

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).

推荐答案

这是一个小例子:

假设您有一个名为 C:TempTest.xml 的文件,其中包含以下内容:

Suppose you have a file called C:TempTest.xml with this contents:

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

然后你就可以使用这个VBScript了:

Then you can use this VBScript:

Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load "C:TempTest.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

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

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

这篇关于在 VBScript 中导航 XML 节点,用于 Dummy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆