使用 VBS 搜索 XML 并更改值 [英] Searching XML using VBS and changing a value
问题描述
我创建了一个用于无人值守 Windows7 安装的应答文件.我希望能够即时修改一些设置(时区、计算机名称等),但我是 VBScript/XML 的新手.我在这个网站上找到了一篇简洁的文章 VBScript 在 XML 节点中找到一个节点并替换其值 关于如何使用 xpath.我的一些麻烦是针对节点(我认为),因为我还没有找到使用格式的示例.我试过使用 full 和 just ,但在完整的答案文件中有几个具有相同组件名称的节点.建议……好吗?:)
I created an answer file to be used for an Unattended Windows7 install. I would like to be able to modify a few settings on the fly (Time Zone, computer name, ect), but I'm new with VBScript/XML. I found a neat artical on this site VBScript Find a node in XML node and replace the value on how to use xpath. Some of my trouble is targeting the node (I think) as I haven't found an example using format. I've tried using the full and just , but in the full answer file there are several nodes with the same component name. Suggestions...please? :)
<unattend xmlns="urn.schemas-microsoft.com:unattend">
<settings pass="specialize">
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="*REMOVED*" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ProductKey>*REMOVED*</ProductKey>
<RegisteredOwner>*REMOVED*</RegisteredOwner>
<DisableAutoDaylightTimeSet>false</DisableAutoDaylightTimeSet>
<ComputerName>*</ComputerName>
<DoNotCleanTaskBar>true</DoNotCleanTaskBar>
<BluetoothTaskbarIconEnabled>false</BluetoothTaskbarIconEnabled>
<CopyProfile>true</CopyProfile>
<ShowWindowsLive>false</ShowWindowsLive>
<TimeZone>VarTime</TimeZone>
</component>
</settings>
</unattend>
与 VB 打交道,我能够自己想出一个人.我很欣赏这个帖子.这也会提示输入用户框.有什么理由为什么这样的事情不起作用并且不能有效地完成工作?
Messing around with VBs, I was able to come up with someone on my own. I do appreciate the post. This prompts for a user box as well. Is there any reason why something like this wouldn't work and do the job efficiently?
Set xml = CreateObject("Msxml2.DOMDocument.3.0")
xml.Async = "False"
xml.load "path.xml"
strTime = InputBox("Please Select your Time Zone.")
strTimeZone = "Nothing"
if strTime= "1" then strTimeZone= "Eastern Standard Time"
if strTime= "2" then strTimeZone= "Central Standard Time"
if strTime= "3" then strTimeZone= "Mountian Standard Time"
if strTime= "4" then strTimeZone= "Pacific Stardard Time"
Set TimeZone = xml.selectSingleNode("//unattend/settings/component/TimeZone")
TimeZone.Text = strTimeZone
'Save the xml document with the new settings.
strResult = xml.save("path.xml")
推荐答案
这个脚本
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 属性和前缀.
shows how to use the SelectionNamespaces property and prefixes in the XPath expression.
附言查看此处了解如何查找/更改属性(使用基本相同的代码).
P.S. Look here to see how to look for/change attributes (with essentially the same code).
P.P.S:
更多相同.
这篇关于使用 VBS 搜索 XML 并更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!