使用VBS搜索XML并更改值

时间:2012-07-30 17:10:19

标签: xml vbscript

我创建了一个用于无人值守Windows7安装的答案文件。我希望能够动态修改一些设置(时区,计算机名称等),但我是VBScript / XML的新手。我在这个网站VBScript Find a node in XML node and replace the value上发现了一个关于如何使用xpath的简洁艺术。我的一些麻烦是针对节点(我认为),因为我没有找到使用格式的示例。我尝试过使用full和just,但在完整的答案文件中有几个节点具有相同的组件名称。建议...吗? :)

<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,我能够独自想出一个人。我非常感谢这篇文章。这也会提示输入用户Box。有什么理由说这样的事情不起作用并且有效地完成工作吗?

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")

1 个答案:

答案 0 :(得分:0)

此脚本

  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。 查看here以了解如何查找/更改属性(基本上使用相同的代码)。

<强> P.P.S:

More同样。