如何从xml文件中使用vbscript提取变量名及其值
问题描述:
我有如下xml文件&我想提取变量名&它的值。你能帮我解决这个问题吗? 我的XML格式是如何从xml文件中使用vbscript提取变量名及其值
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MyApps.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="MyApps.Properties.Settings.dbConnString" connectionString="Data Source=MSTEST01\TQA;Initial Catalog=TQA;Persist Security Info=True;User ID=UserID;Password=Pwd"
providerName="System.Data.SqlClient" />
</connectionStrings>
<applicationSettings>
<MyApps.Properties.Settings>
<setting name="BasePath" serializeAs="String">
<value>\\Results</value>
</setting>
<setting name="cPath" serializeAs="String">
<value>C:\Controller</value>
</setting>
<setting name="ePath" serializeAs="String">
<value>C:\Debug\E.exe</value>
</setting>
<setting name="fPath" serializeAs="String">
<value>C:\Framework</value>
</setting>
<setting name="engineId" serializeAs="String">
<value>1</value>
</setting>
<setting name="wPath" serializeAs="String">
<value>C:\S5</value>
</setting>
</MyApps.Properties.Settings>
</applicationSettings>
</configuration>
,我期待像例如输出executablePath = “d:\ MYEXE.EXE”
在此先感谢
答
终于让我找到我的答案,可以有人建议如果有更好的选择?以下是我的脚本
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = "False"
xmlDoc.Load ("C:\STEPRunRequest\STEPRunner.exe.config")
strQuery = "/configuration/applicationSettings/Eunner.Properties.Settings/*"
Set colItem = xmlDoc.SelectNodes(strQuery)
Set xmlElement = xmlDoc.DocumentElement.SelectSingleNode("/configuration/configSections[@connectionStrings]") '='" <SomeValue> & "'
Set queryNode = xmlDoc.SelectSingleNode(".//node()[@connectionString]")
MsgBox queryNode.Attributes(1).Text
For Each objItem In colItem
If objItem.Attributes.Length > 0 Then
MsgBox objItem.Attributes(0).Text & ": " & objItem.Text
End If
Next
答
您也可以尝试以下操作。利用getAttribute - Cscript vbs。
Set objXML2 = CreateObject("Microsoft.XMLDOM")
objXML2.async = "false"
strdir="c:\config.xml"
If (Not objXML2.load(strdir)) Then
wscript.echo "Unable to load file '" & strdir & "'. "
WScript.Quit(1)
End If
Set colNodes = objXML2.selectNodes ("/configuration/applicationSettings/Eunner.Properties.Settings")
For Each objNode in colNodes
wscript.echo objnode.getAttribute("name")& " : " & objNode.text
Next
第2分[这个答案](http://stackoverflow.com/a/2181953/69820)应该让你开始 – 2012-04-04 07:59:51
@Nathan赖斯我尝试下面的代码,并能够获得所需的值, (“C:\ test \ myxml.xml”) strQuery =“变量,但坚持retreive connectionstring变量及其值/configuration/applicationSettings/MyApps.Properties.Settings/*“ Set colItem = xmlDoc.SelectNodes(strQuery) For Each objItem IncolItem If if objItem.Attributes.Length> 0 Then MsgBox objItem.Attributes(0).Text&“:”&objItem.Text End If Next – Shree 2012-04-05 05:20:22