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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can you edit xml with VBS 1

Status
Not open for further replies.

BeatTheSystem

Technical User
May 23, 2011
2
0
0
US
the document is located at C:\ProgramData\Lightspeed Systems\LsGuide\LsGuide.xml
i would like to change the "block on hidden" string from true to
false or from false to true. if you can post a complete script to
edit this file at this location that would be great.


<?xml version="1.0" encoding="utf-8" ?>
- <LsGuide platform="Windows" release="15">
<MachineGuid>67379DF1-76FA-45A3-ADAD-1E292DE07175</MachineGuid>
- <ServerNames>
<ServerName>lightspeed.henrico.k12.va.us</ServerName>
</ServerNames>
<WorkingDllFileName>C:\Program Files\Lightspeed Systems\Lightspeed Guide\LSG0001.DLL</WorkingDllFileName>
<HideGuide>true</HideGuide>
<SafeSearch>true</SafeSearch>
<BlockOnFailure>false</BlockOnFailure>
<MachineCode>83DF5DA126281F782AA296887EB224D57FC371B4</MachineCode>
<SecurityCode />
<CheckSum>435416</CheckSum>
</LsGuide>
 
oops wow i ment <BlockOnFailure>false</BlockOnFailure> don't see how i got hidden from failure. thanks for any help you can give
 
IMHO, the solution is to read the original XML-file line by line and write the copy in another XML-file.
In every line search for the pattern
<BlockOnFailure>false</BlockOnFailure>
and when found replace it with
<BlockOnFailure>true</BlockOnFailure>
 
Here is a simple example:
Given is this input file
xml_input.xml
Code:
<?xml version="1.0" encoding="utf-8" ?> 
- <LsGuide platform="Windows" release="15">
  <MachineGuid>67379DF1-76FA-45A3-ADAD-1E292DE07175</MachineGuid> 
- <ServerNames>
  <ServerName>lightspeed.henrico.k12.va.us</ServerName> 
  </ServerNames>
  <WorkingDllFileName>C:\Program Files\Lightspeed Systems\Lightspeed Guide\LSG0001.DLL</WorkingDllFileName> 
  <HideGuide>true</HideGuide> 
  <SafeSearch>true</SafeSearch> 
  <BlockOnFailure>[COLOR=red][b]false[/b][/color]</BlockOnFailure> 
  <MachineCode>83DF5DA126281F782AA296887EB224D57FC371B4</MachineCode> 
  <SecurityCode /> 
  <CheckSum>435416</CheckSum> 
  </LsGuide>
Then runnig this script
xml_modify.vbs
Code:
[COLOR=#804040][b]set[/b][/color] oFSO [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]CreateObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Scripting.FileSystemObject"[/color][COLOR=#804040][b])[/b][/color]

[COLOR=#804040][b]const[/b][/color] ForReading [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color][COLOR=#804040][b],[/b][/color] ForWriting [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b],[/b][/color] ForAppending [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]8[/color] 
[COLOR=#0000ff]'open the input file for reading[/color]
[COLOR=#804040][b]set[/b][/color] oInFile [COLOR=#804040][b]=[/b][/color] oFSO[COLOR=#804040][b].[/b][/color]OpenTextFile[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"xml_input.xml"[/color][COLOR=#804040][b],[/b][/color] ForReading[COLOR=#804040][b])[/b][/color]
[COLOR=#0000ff]'open the output file for writing[/color]
[COLOR=#804040][b]set[/b][/color] oOutFile [COLOR=#804040][b]=[/b][/color] oFSO[COLOR=#804040][b].[/b][/color]OpenTextFile[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"xml_output.xml"[/color][COLOR=#804040][b],[/b][/color] ForWriting[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]True[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#0000ff]'for each line in the input file[/color]

[COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] [COLOR=#804040][b]not[/b][/color] oInFile[COLOR=#804040][b].[/b][/color]AtEndOfStream
 [COLOR=#0000ff] 'read the line[/color]
  [COLOR=#a020f0]line[/color] [COLOR=#804040][b]=[/b][/color] oInFile[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]ReadLine[/color][COLOR=#804040][b]()[/b][/color]
  [COLOR=#a020f0]line[/color] [COLOR=#804040][b]=[/b][/color] process_line[COLOR=#804040][b]([/b][/color][COLOR=#a020f0]line[/color][COLOR=#804040][b])[/b][/color]
  oOutFile[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]WriteLine[/color][COLOR=#804040][b]([/b][/color][COLOR=#a020f0]line[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]loop[/b][/color]
[COLOR=#0000ff]'close the input file[/color]
oInFile[COLOR=#804040][b].[/b][/color][COLOR=#804040][b]close[/b][/color]
[COLOR=#0000ff]'close the outut file[/color]
oOutFile[COLOR=#804040][b].[/b][/color][COLOR=#804040][b]close[/b][/color]

[COLOR=#804040][b]function[/b][/color] process_line[COLOR=#804040][b]([/b][/color][COLOR=#a020f0]line[/color][COLOR=#804040][b])[/b][/color]
  [COLOR=#804040][b]set[/b][/color] re [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]new[/b][/color] regexp
  re[COLOR=#804040][b].[/b][/color]pattern [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"<\s*BlockOnFailure\s*>\s*false\s*<\s*/BlockOnFailure\s*>"[/color]
  [COLOR=#804040][b]if[/b][/color] re[COLOR=#804040][b].[/b][/color]test[COLOR=#804040][b]([/b][/color][COLOR=#a020f0]line[/color][COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color]
    process_line [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"<BlockOnFailure>true</BlockOnFailure>"[/color]
  [COLOR=#804040][b]else[/b][/color]
    process_line [COLOR=#804040][b]=[/b][/color] [COLOR=#a020f0]line[/color]
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]function[/b][/color]
with the command
Code:
cscript /NoLogo xml_modify.vbs
produces in the same directory the file
xml_output.xml
Code:
<?xml version="1.0" encoding="utf-8" ?> 
- <LsGuide platform="Windows" release="15">
  <MachineGuid>67379DF1-76FA-45A3-ADAD-1E292DE07175</MachineGuid> 
- <ServerNames>
  <ServerName>lightspeed.henrico.k12.va.us</ServerName> 
  </ServerNames>
  <WorkingDllFileName>C:\Program Files\Lightspeed Systems\Lightspeed Guide\LSG0001.DLL</WorkingDllFileName> 
  <HideGuide>true</HideGuide> 
  <SafeSearch>true</SafeSearch> 
<BlockOnFailure>[COLOR=green][b]true[/b][/color]</BlockOnFailure>
  <MachineCode>83DF5DA126281F782AA296887EB224D57FC371B4</MachineCode> 
  <SecurityCode /> 
  <CheckSum>435416</CheckSum> 
  </LsGuide>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top