views:

1022

answers:

4

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

+1  A: 

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.

Joel Coehoorn
A: 

Hi Joel, thanks for the suggestion. Are you able to provide me with a pointer, as I've studied the link for a while and to my untrained eye I cannot see anything that would help me achieve what I need to do?

A: 

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.

Tmdean
A: 

Thanks very much, this look a bit more like it. The XML file I am editing has this info at the top of it, I wonder if the version is incompatible:

<?xml version="1.0" encoding="UTF-8"?>

and the tags are in this format:

<container name="root">

Is this ok for your suggestion? I've put this together to try to test whether I'm getting the correct string returned, but I get an error about the object "password" being required:

Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.Load "config.xml"
Set password = xml.SelectSingleNode("//root/EIMServer/Global/OldConfig/K2097153")
wscript.echo password.text

Note that I have removed all of the "container name=" bits out of the SelectSingleNode. Is the XML version of the file incompatible?

The error message that you're getting indicates that your XPath query isn't finding anything. I can't help that much unless I know the exact structure of the XML document you're trying to read, but I don't think your query should start with //root unless the document actually starts with <root>.
Tmdean
Also, don't worry about the <? stuff. That's just an XML processing instruction, and will be pretty much ignored by the parser.
Tmdean