Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

vbscript - Searching XML using VBS and changing a value

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>

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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This script

  Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
  Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..estdataxml
s-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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...