Reputation:
I have a configuration XML file which contains an encrypted password. From time to time we need to reset this password, and the methodology for doing so involves stripping out the string which is effectively the password from the XML file and saving the file.
The process also involves stopping and restarting a couple of services which I can manage, but I am yet to figure out a way of searching and replacing the string, as it can be different every time.
I'd like to do this in VBS (because that's pretty much all I am familiar with in the scripting world) but am happy to consider doing it any other way.
I have looked at the Replace function, but I am yet to find a way of putting a wildcard into the search criteria. I need to be able to search for:
<A>randomstuff</A>
and replace with:
<A></A>
For those who may be familiar with the situation, I am resetting the password for Websense Enterprise Manager.
The XML file appears to be v1.0:
<?xml version="1.0" encoding="UTF-8"?>
Thanks. Tom
Upvotes: 0
Views: 5768
Reputation: 361
Or if your on a Windows machine it might be even quicker and easier using a single-line powershell command
(Get-Content doc.xml) -replace '<a>.*</a>','<a></a>' | Set-Content doc.xml
And then you get to learn to Powershell along the way to add to your CV!
Upvotes: 0
Reputation: 9309
Never use regular expressions to parse XML. They are far from suitable for the task. The solution you should be looking for is how to parse XML with VBScript, which is pretty simple.
Here's an example script that will parse an XML file and replace the text in a specific password element.
Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.Load "C:\doc.xml"
Set password = xml.SelectSingleNode("//root/element/password")
password.Text = "Hello, world"
xml.Save "C:\doc.xml"
Here's an example XML file that the script would work on.
<root>
<element>
<password>Example password</password>
</element>
</root>
The important part of the script is the "SelectSingleNode" method. This uses XPath syntax to search through the XML document for the element we want. XPath syntax is pretty simple and any number of references for it can be found online.
Upvotes: 1
Reputation: 416179
Perhaps a regular expression?
I normally wouldn't recommend them for parsing xml/html files, but if your xml is very specific you might be okay.
Upvotes: 1