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

查看:110
本文介绍了在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中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 Documents\cds.xml") Then
   msgbox("Success!")
Else
   msgbox("Failure!")
End If

每次都会返回失败。

基于另一种方法

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

然后我尝试了几件事情来检测它是否像消息框fo r每个节点名称在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.

我知道我的问题可能看起来很愚蠢和无知,因为这应该是很容易获得的信息,但我真的不能为我的生活找到基本知识我需要了解如何使用JUST 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:\Temp\Test.xml 与此内容:

Suppose you have a file called C:\Temp\Test.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:\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

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

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

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

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