Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
791 views
in Technique[技术] by (71.8m points)

xml - selectSingleNode using vbscript

Below is the structure of my xml file:

<configuration>
  <appSettings>
    <add key="ProductVersion" value="5.5.5"/>
    <add key="LogsDirectory" value="e:\Logs"/>
  </appSettings>
<configuration>

I am trying following code to get value of LogsDirectory:

configurationFilePath = "e:conf.xml"
Set xmlDoc = CreateObject("MSXML2.DomDocument.6.0")
xmlDoc.async = false
Call xmlDoc.load(configurationFilePath)

xpath1 = ".//configuration/appSettings/add[@key='LogsDirectory']/@value"
LogsDirectory = xmlDoc.selectSingleNode(xpath1)

But it is giving error as object required.

Any help highly appreciated.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

People who not use an error checking skeleton for XML work like this:

Option Explicit

Dim oFS    : Set oFS   = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec    = oFS.GetAbsolutePathName(".19194544.xml")
Dim oXDoc  : Set oXDoc = CreateObject("MSXML2.DomDocument.6.0")
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load sFSpec

If 0 = oXDoc.ParseError Then
   WScript.Echo sFSpec, "looks ok"
   Dim sXPath
   For Each sXpath In Array( _
       ".//configuration/appSettings/add[@key='LogsDirectory']/@value" _
   )
       Dim ndFnd : Set ndFnd = oXDoc.selectSingleNode(sXpath)
       If Not ndFnd Is Nothing Then
          WScript.Echo "found |" & ndFnd.xml & "|"
       Else
          WScript.Echo "not found |" & sXPath & "|"
       End If
   Next
Else
   WScript.Echo oXDoc.ParseError.Reason
End If

also do bungee jumping without ropes.

In your case the .ParseError.Reason

The following tags were not closed: configuration, configuration.

explains why there is no document to search in. At least this spares you the error you'd get when you try to assign the node returned from .selectSingleNode() to LogsDirectory without using Set.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...