使用VBS搜索XML并更改值
我创建了一个应答文件,用于无人参与的Windows7安装。我希望能够动态地修改一些设置(时区、计算机名等),但我不熟悉VBScript/XML。我在这个网站上找到了一篇关于如何使用xpath的文章。我的一些问题是针对节点(我想),因为我还没有找到使用格式的示例。我尝试过使用full和just,但是在full-answer文件中有几个节点具有相同的组件名称。建议……请?:) 这个剧本使用VBS搜索XML并更改值,xml,vbscript,Xml,Vbscript,我创建了一个应答文件,用于无人参与的Windows7安装。我希望能够动态地修改一些设置(时区、计算机名等),但我不熟悉VBScript/XML。我在这个网站上找到了一篇关于如何使用xpath的文章。我的一些问题是针对节点(我想),因为我还没有找到使用格式的示例。我尝试过使用full和just,但是在full-answer文件中有几个节点具有相同的组件名称。建议……请?:) 这个剧本 Dim oFS : Set oFS = CreateObject("Scripting.FileSys
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName("..\testdata\xml\ns-xpath-01.xml")
Dim sNS : sNS = "xmlns:a='urn.schemas-microsoft.com:unattend'"
Dim oXML : Set oXML = CreateObject("Msxml2.DOMDocument")
oXML.setProperty "SelectionLanguage", "XPath"
oXML.setProperty "SelectionNamespaces", sNS
oXML.async = False
oXML.load sFSpec
If 0 = oXML.parseError Then
WScript.Echo oXML.xml
WScript.Echo "-----------------"
Dim sXPath : sXPath = "/a:unattend/a:settings/a:component/a:TimeZone"
Dim ndFnd : Set ndFnd = oXML.selectSingleNode(sXPath)
If ndFnd Is Nothing Then
WScript.Echo sXPath, "not found"
Else
WScript.Echo ndFnd.text
WScript.Echo "-----------------"
ndFnd.text = "Abracadabra"
WScript.Echo oXML.xml
End If
Else
WScript.Echo oXML.parseError.reason
End If
输出:
<unattend xmlns="urn.schemas-microsoft.com:unattend">
<settings pass="specialize">
<component>
<TimeZone>VarTime</TimeZone>
</component>
</settings>
</unattend>
-----------------
VarTime
-----------------
<unattend xmlns="urn.schemas-microsoft.com:unattend">
<settings pass="specialize">
<component>
<TimeZone>Abracadabra</TimeZone>
</component>
</settings>
</unattend>
瓦泰姆
-----------------
瓦泰姆
-----------------
阿布拉卡达拉
演示如何在XPath表达式中使用SelectionNamespaces属性和前缀
p.S.
查看如何查找/更改属性(使用基本相同的代码)
p.p.S:
同样的。谢谢您的反馈。我能想出一些(如上所述)似乎效果很好的办法。现在,我在Inputbox()方面遇到了问题,并确保存在正确的值。如果我想不出来,我会再问一个问题。
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName("..\testdata\xml\ns-xpath-01.xml")
Dim sNS : sNS = "xmlns:a='urn.schemas-microsoft.com:unattend'"
Dim oXML : Set oXML = CreateObject("Msxml2.DOMDocument")
oXML.setProperty "SelectionLanguage", "XPath"
oXML.setProperty "SelectionNamespaces", sNS
oXML.async = False
oXML.load sFSpec
If 0 = oXML.parseError Then
WScript.Echo oXML.xml
WScript.Echo "-----------------"
Dim sXPath : sXPath = "/a:unattend/a:settings/a:component/a:TimeZone"
Dim ndFnd : Set ndFnd = oXML.selectSingleNode(sXPath)
If ndFnd Is Nothing Then
WScript.Echo sXPath, "not found"
Else
WScript.Echo ndFnd.text
WScript.Echo "-----------------"
ndFnd.text = "Abracadabra"
WScript.Echo oXML.xml
End If
Else
WScript.Echo oXML.parseError.reason
End If
<unattend xmlns="urn.schemas-microsoft.com:unattend">
<settings pass="specialize">
<component>
<TimeZone>VarTime</TimeZone>
</component>
</settings>
</unattend>
-----------------
VarTime
-----------------
<unattend xmlns="urn.schemas-microsoft.com:unattend">
<settings pass="specialize">
<component>
<TimeZone>Abracadabra</TimeZone>
</component>
</settings>
</unattend>