Searching XML using VBS and changing a value

前端 未结 1 1741
南笙
南笙 2020-12-12 04:24

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

相关标签:
1条回答
  • 2020-12-12 05:01

    This script

      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
    

    output:

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

    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:

    More of the same.

    0 讨论(0)
提交回复
热议问题