使用 vbscript 选择单节点 [英] selectSingleNode using vbscript

查看:12
本文介绍了使用 vbscript 选择单节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的xml文件的结构:

<预><代码><配置><应用设置><add key="ProductVersion" value="5.5.5"/><add key="LogsDirectory" value="e:\\Logs"/></appSettings><配置>

我正在尝试使用以下代码来获取 LogsDirectory 的值:

configurationFilePath = "e:\conf.xml"设置 xmlDoc = CreateObject("MSXML2.DomDocument.6.0")xmlDoc.async = 假调用 xmlDoc.load(configurationFilePath)xpath1 = ".//configuration/appSettings/add[@key='LogsDirectory']/@value"LogsDirectory = xmlDoc.selectSingleNode(xpath1)

但它给出了需要对象的错误.

非常感谢任何帮助.

谢谢

解决方案

不使用 XML 错误检查框架的人是这样工作的:

选项显式Dim oFS:设置 oFS = CreateObject("Scripting.FileSystemObject")Dim sFSpec : sFSpec = oFS.GetAbsolutePathName(".\19194544.xml")Dim oXDoc : 设置 oXDoc = CreateObject("MSXML2.DomDocument.6.0")oXDoc.setProperty "SelectionLanguage", "XPath"oXDoc.async = FalseoXDoc.load sFSpec如果 0 = oXDoc.ParseError 那么WScript.Echo sFSpec,看起来不错"Dim sXPath对于数组中的每个 sXpath( _".//configuration/appSettings/add[@key='LogsDirectory']/@value" _)Dim ndFnd : 设置 ndFnd = oXDoc.selectSingleNode(sXpath)如果不是 ndFnd 什么都不是WScript.Echo "找到 |"&ndFnd.xml &|"别的WScript.Echo "未找到 |"&sXPath &|"万一下一个别的WScript.Echo oXDoc.ParseError.Reason万一

也可以不用绳子做蹦极.

在你的情况下 .ParseError.Reason

以下标签没有关闭:配置,配置.

解释了为什么没有可搜索的文档.至少这可以避免在您尝试将 .selectSingleNode() 返回的节点分配给 LogsDirectory 而不使用 Set 时会出现的错误.

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

解决方案

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.

这篇关于使用 vbscript 选择单节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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