Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating XML value 1

Status
Not open for further replies.

HVtech

IS-IT--Management
Jul 1, 2010
71
NL
Hi,
I'm trying to update a value which is stored in a xml file.
Preferable through a popup where the input (new value) is typed in.
Any help is much aprreciated!
This is what I have found, but not working..
Code:
'Define what our new value will be and place it into a variable
sValue = "Testvalue"

'Get path of script, (scriptfullname), then replace the scriptname with nothing  bring us only the path and place it into strScriptPath
strScriptPath = replace(wscript.scriptfullname,wscript.scriptname,"")

'Create XMLDoc object
Set xmlDoc = CreateObject("Microsoft.XMLDOM")

'load up the XML file and place it into xmldoc
xmlDoc.load strScriptPath & "config.xml"

'Select the node we want to edit
'The text IS case sensitive
Set sInboxFolder = xmlDoc.selectsinglenode ("registration")

'Show the current value in FaxInBox
msgbox sInboxFolder.text

'Set the text node with the new value
sInboxFolder.text = sValue

'Save the xml document with the new settings.
iResult = xmldoc.save(strScriptPath & "config.xml")

'You can use the iResult as a way to determine if the file gets saved correctly or not (‘0=yes’, ‘1=no’)
 
I'm almost ther I think with this piece of code.
Who helps me getting the final touch ?

Code:
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("config.xml")

Set ElemList = objXMLDoc.getElementsByTagName("registration")
plot = ElemList.item(0).Text

strInput = UserInput( "Nieuwe image naam:" )
WScript.Echo "Nieuwe imagenaam in XML: " & strInput

Set NewText = strInput
Set Category = ElemList
Set OldText = plot
Category.replaceChild NewText, OldText

objXMLDoc.save("config.xml")
 
[tt]'etc etc

strInput = [red]InputBox[/red]( "Nieuwe image naam:" )
WScript.Echo "Nieuwe imagenaam in XML: " & strInput

[red]'[/red]Set NewText = strInput
[red]'[/red]Set Category = ElemList
[red]'[/red]Set OldText = plot
[red]'[/red]Category.replaceChild NewText, OldText
[red]ElemList.item(0).text=strInput[/red]

'etc etc[/tt]
 
Thanks tsuji, works like a charm :)
 
Got one more question..
Now I have defined which xml file to load
objXMLDoc.load("config.xml")
I have several xml files with names like 241.xml, 108.xml etc.
I would like to have the choice to load which xml file, also via inputbox.. is this possible ?
 
This is the piece of the XML files I want to adjust..
<?xml version="1.0" ?>

<ImageSelection>
<WillShowUI>OnError</WillShowUI>
<InstallImage>
<ImageName>HPD530</ImageName>
<ImageGroup>InstallGroup</ImageGroup>
<Filename>HPD530.wim</Filename>
</InstallImage>
Suddenly I get errors when I'm adjusting the vbs script to the right xml.. see above..
????

Would be nice if the xml file name was an input too
This is script so far:
Code:
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("Unattendx86.xml")

Set ElemList = objXMLDoc.getElementsByTagName("InstallImage")
plot = ElemList.item(0).Text

strInput = UserInput( "Nieuwe image naam:" )
WScript.Echo "Nieuwe imagenaam in XML: " & strInput

ElemList.item(0).text=strInput

'Tweede variable
Set ElemList = objXMLDoc.getElementsByTagName("Filename")
plot2 = ElemList.item(0).Text

ElemList.item(0).text=strInput & ".wim"

objXMLDoc.save("Unattendx86.xml")

Function UserInput( myPrompt )
' This function prompts the user for some input.
' When the script runs in CSCRIPT.EXE, StdIn is used,
' otherwise the VBScript InputBox( ) function is used.
' myPrompt is the the text used to prompt the user for input.
' The function returns the input typed either on StdIn or in InputBox( ).
' Written by Rob van der Woude
' [URL unfurl="true"]http://www.robvanderwoude.com[/URL]
    ' Check if the script runs in CSCRIPT.EXE
    If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
        ' If so, use StdIn and StdOut
        WScript.StdOut.Write myPrompt & " "
        UserInput = WScript.StdIn.ReadLine
    Else
        ' If not, use InputBox( )
        UserInput = InputBox( myPrompt )
    End If
End Function
Thanks for helping out..
 
>Set ElemList = objXMLDoc.getElementsByTagName("InstallImage")
[tt]Set ElemList = objXMLDoc.getElementsByTagName("[red]ImageName[/red]")[/tt]
 
Hi Tsuji
Thanks again but I'm getting an error on line 6:
Object required: 'Elemlist.Item(....)
code: 800A01A8
That's why I was changing the Tagname to see if this was the problem..
Weird, cause earlier with the config.xml, which had simple layout (no Child nodes) the script worked..
Any ideas?
And how to catch the xml file name to load from userinput?
Or is there a way to enumerate a directory where the xml files are stored, so I can select the appropriate file to edit?
 
It can error out only if your xml is not well-formed, then the .load() has already failed. Is the ImageSelection actually closed with </ImageSelection> or it is just a typo?
 
Huh? What do you mean? Not following what you say..
 
[tt] <ImageSelection>
<WillShowUI>OnError</WillShowUI>
<InstallImage>
<ImageName>HPD530</ImageName>
<ImageGroup>InstallGroup</ImageGroup>
<Filename>HPD530.wim</Filename>
</InstallImage>
[red]</ImageSelection>[/red][/tt]
 
Oh now I see what you mean..
But the closing is in the XML file, but since these lines aren't relevant I didn't post them.
Here you see..:
<ImageSelection>
<WillShowUI>OnError</WillShowUI>
<InstallImage>
<ImageName>HP-D530SFF_03</ImageName>
<ImageGroup>InstallGroup</ImageGroup>
<Filename>HPD530-03.wim</Filename>
</InstallImage>
<InstallTo>
<DiskID>0</DiskID>
<PartitionID>1</PartitionID>
</InstallTo>
</ImageSelection>

So this was not the cause of the error..
 
Check all spelling, upper/lower cases. Make sure your function UserInput() is working. Add this line after the .load().
[tt] wscript.echo objXMLDoc.xml[/tt]
 
File is not loading..
I renamed to crosscheck, both cases no output from wscript.echo objXMLDoc.xml

Can't we treat this file as text file ?
Need your help again for the code.. sorry to bother.
Appreciate your help very much tough
 
Check the path to the xml file. As of the present form, the file should be in the same directory of the vbs file. I presume due diligent of your part to avoid entanglement of the sort.
 
Both files, XML and vbs are in the same directory.
To double check I set the complete path D:\Script\Unattend.xml
in the vbs.
No difference, same error as before.
 
Post the complete and truthful xml. You don't have a well-formed xml.
 
Oke, here it comes:
Unattend.xml
Code:
<?xml version="1.0" ?> 
<unattend xmlns="urn:schemas-microsoft-com:unattend">
   <settings pass="windowsPE">
      <component name="Microsoft-Windows-Setup" publicKeyToken="31bf3856ad364e35" 
language="neutral" versionScope="nonSxS" processorArchitecture="x86">
         <WindowsDeploymentServices>
            <Login>
               <WillShowUI>OnError</WillShowUI>
               <Credentials>
                     <Username>******</Username>
                     <Domain>*******</Domain>
                     <Password>*******</Password>
                  </Credentials>
               </Login>
            <ImageSelection>
               <WillShowUI>OnError</WillShowUI>
               <InstallImage>
                  <ImageName>HP-D530SFF_03</ImageName>
                  <ImageGroup>InstallGroup</ImageGroup>
                  <Filename>HPD530-03.wim</Filename>
               </InstallImage>
                  <InstallTo>
                  <DiskID>0</DiskID>
                  <PartitionID>1</PartitionID>
               </InstallTo>
            </ImageSelection>
         </WindowsDeploymentServices>
         <DiskConfiguration>
                <Disk wcm:action="add">
                    <CreatePartitions>
                        <CreatePartition wcm:action="add">
                            <Order>1</Order>
                            <Size>27200</Size>
                            <Type>Primary</Type>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>2</Order>
                            <Type>Extended</Type>
                            <Extend>true</Extend>
                        </CreatePartition>
                        <CreatePartition wcm:action="add">
                            <Order>3</Order>
                            <Type>Logical</Type>
                            <Extend>true</Extend>
                        </CreatePartition>
                    </CreatePartitions>
                    <ModifyPartitions>
                        <ModifyPartition wcm:action="add">
                            <Active>true</Active>
                            <Extend>false</Extend>
                            <Format>NTFS</Format>
                            <Label>WinXP</Label>
                            <Letter>C</Letter>
                            <Order>1</Order>
                            <PartitionID>1</PartitionID>
                        </ModifyPartition>
                        <ModifyPartition wcm:action="add">
                            <Active>false</Active>
                            <Extend>false</Extend>
                            <Format>NTFS</Format>
                            <Label>Rommelen</Label>
                            <Letter>D</Letter>
                            <Order>2</Order>
                            <PartitionID>2</PartitionID>
                        </ModifyPartition>
                    </ModifyPartitions>
                    <WillWipeDisk>true</WillWipeDisk>
                    <DiskID>0</DiskID>
                </Disk>
                <WillShowUI>OnError</WillShowUI>
            </DiskConfiguration>
      </component>
      <component name="Microsoft-Windows-International-Core-WinPE" publicKeyToken="31bf3856ad364e35" 
language="neutral" versionScope="nonSxS" processorArchitecture="x86">
         <SetupUILanguage>
            <WillShowUI>OnError</WillShowUI>
            <UILanguage>en-US</UILanguage>
         </SetupUILanguage>
         <UILanguage>nl-NL</UILanguage>
      </component>
   </settings>
</unattend>

Complete SetImage.vbs:
Code:
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("Unattended.xml")
wscript.echo objXMLDoc.xml 

Set ElemList = objXMLDoc.getElementsByTagName("ImageName")  
plot = ElemList.item(0).Text
WScript.Echo plot

strInput = UserInput( "Nieuwe image naam:" )
WScript.Echo "Nieuwe imagenaam in XML: " & strInput

ElemList.item(0).text=strInput

'Tweede variable
Set ElemList = objXMLDoc.getElementsByTagName("Filename")
plot2 = ElemList.item(0).Text
ElemList.item(0).text=strInput & ".wim"

objXMLDoc.save("Unattended.xml")

Function UserInput( myPrompt )
' This function prompts the user for some input.
' When the script runs in CSCRIPT.EXE, StdIn is used,
' otherwise the VBScript InputBox( ) function is used.
' myPrompt is the the text used to prompt the user for input.
' The function returns the input typed either on StdIn or in InputBox( ).
' Written by Rob van der Woude
' [URL unfurl="true"]http://www.robvanderwoude.com[/URL]
    ' Check if the script runs in CSCRIPT.EXE
    If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
        ' If so, use StdIn and StdOut
        WScript.StdOut.Write myPrompt & " "
        UserInput = WScript.StdIn.ReadLine
    Else
        ' If not, use InputBox( )
        UserInput = InputBox( myPrompt )
    End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top