Search XML in VBscript

来源:互联网 发布:如何加入淘宝客做推广 编辑:程序博客网 时间:2024/05/29 19:52

Recently, in my project, there is a requirment that need to search specific XML node by using vbscript, in that, meet several issues and work out finally, so record these here.

 

1. MSXML Version

As we know, there are several versions of MSXML from 3.0 to 6.0, here is link to provides detail.

In my opinion, you'd better choose V6.0 if possible, it supports Xpath friendly.

 

2. How to use MSXML in VBScript

 

Dim MSXML
Set MSXML = CreateObject("MSXML2.DOMDocument.6.0") 'Create the MSXML object

 

There are also some basic properites you need to configure before you use the object

Async = true

preserverWhiteSpace = true

validateOnParse = true


 

here is one properties I like to highlight, it is

MSXML.setProperty "SelectionLanguage", "XPath"

 

It lets you can use XPATH in your code.

 

And if in your xml file, there is xml namespace, you also have to set

MSXML.setProperty"SelectionNamespaces","xmlns:prefix='http://www.gbxml.org/schema'"

 

After you set the proper properties of MSXML object, you can use Load() to load your xml file

MSXML.Load("/Analyzed.xml")

 

Next, it is very easy, like you use xpath in C# code, there are two methods let you select the specific XML node,

SelectSingleNode(SearchString)

SelectNodes( SearchString )