XML node navigation in VBScript, for Dummy

I am trying to write a script that will process some data for me in an XML file. I am new to VBScript but have experience with VB.NET and VBA, so I feel like I know what I'm doing.

I thought that probably the best way to navigate the file, and not a lot of calling InStr () or similar for each line, to see if there is something I'm looking for. My initial idea was to use several methods that I saw in VB.NET from System.XML, since I saw node navigation functions and elements in this.

After exploring this question, I cannot find a way to import the namespace (System.XML or otherwise) into VBScript without running it on a web page. I decided to look for other options, instead of spending more time looking for this.

It turns out there are other ways to do what I want, using methods and objects that specifically relate to the navigation nodes of the XML file. I found out that some common examples of these “systems” (due to the lack of a better term, because I'm sure it is inappropriate) seem to be DOM and XPath.

I started by learning XPath (since I saw XPath considered superior to the DOM in several places, for example: Moving all nodes in an XML file using VBScript ), I could not find anything to describe the basics of XPath in vbscript. There is a lot of syntax for paths, etc., but I could not find anything that describes the very basics of how to actually call this syntax in VBScript to use it. So I moved on to the next option.

Then I found many different articles / questions / etc. about the DOM. So I tried. None of them worked, all this gave me errors. Basically, it seemed like the DOM object was never loading correctly. Here are just a few of the methods I've tried for this:

From MSDN: Beginner's Guide to the 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 an error 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 determine if it works like message boxes for each node name in xmlDom.documentElement.

I tried so many other things that I don’t even remember.

I just don't know what else I can try or why this does not work for me. I'm just at a loss for what I can try differently, with a syntax that MAY work.

So my question is: how can I navigate an XML file using VBScript without a script embedded in a web page or otherwise? I need to know the basics.

I know that my question probably seems stupid and ignorant, because it should be easily accessible information, but I really can’t let my life find the basics that I need to understand how to navigate nodes in ANY WAY using JUST VBScript (not in html or asp file or something like that).

+6
source share
1 answer

Here is a small example:

Suppose you have a file named C:\Temp\Test.xml with this content:

 <?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:

 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 

Hope this helps you get started with VBScript and XML.

+15
source

Source: https://habr.com/ru/post/910828/


All Articles