0

I am looking to write a program that searches for the tags in an xml document and changes the string between the tags from localhost to manager. The tag might appear in the xml document multiple times, and the document does have a definite path. Would python or vbscript make the most sense for this problem? And can anyone provide a template so I can get started? That would be great. Thanks.

4
  • Is python a requirement? If you run on *X, you can simply use sed. Indeed, it can be done using python. Commented Nov 16, 2010 at 20:10
  • @khachik: Five minutes in the penalty box for suggesting to edit XML with sed. Commented Nov 16, 2010 at 20:15
  • @khachik: No, regexes are not a tool for processing XML, even for something this minor. Commented Nov 16, 2010 at 20:16
  • @Ignacio @delnan sometimes replacing tags doesn't fall into the "processing XML" definition. Commented Nov 16, 2010 at 20:21

5 Answers 5

2

You can solve this problem in almost all languages including Python and Vbscript.

How ever it will be nicer to have the script in python or other languages that have quite a number of XML processing libraries.

If you are just searching for tags, you can use beautifulsoup.

Sign up to request clarification or add additional context in comments.

2 Comments

I am not just searching for tags. I need it to change to 'manager' between the ManagerName tags if it says 'localhost'
@ben: XSLT solution is straight forward but you can definitely do that with beautifulsoup and other python xml libs.
2

I'd use XSLT for this. How you invoke the XSLT is up to you, but libxslt comes with xsltproc.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="//sometag">
    <sometag>manager</sometag>
  </xsl:template>
</xsl:stylesheet>

2 Comments

Although that wouldn't care if the tag contains "localhost", would it?
@delnan: It's just a matter of adding the appropriate predicate. Something like [text()='localhost'].
0

Whether to use vbscript or python is really a question of what makes sense given the environment you're in, the systems you're working on, and the requirements of your company/client.

An xml document template may help either way, but I'd lean toward Python for parsing the xml directly, as a personal preference.

Some helpful examples I started with, can be found here: http://www.xml.com/pub/a/2002/12/11/py-xml.html

Though they don't address your specific problem, they might help get you started.

1 Comment

I should add that I plan on running this on many remote machines, possibly with psexec. I orginally wrote this as a vbscript but am having difficulty with passing it with psexec. That is why I thought python or batch would be better options.
0

Here's a specific VB example that does exactly what you are asking for. It can easily be converted into VBScript and uses the MSXML2.DOMDocument COM object. Un

Dim doc
Dim nlist
Dim node

Set doc = CreateObject("MSXML2.DOMDocument")
doc.setProperty "SelectionLanguage", "XPath"
doc.Load "c:\books.xml"
Set nlist = doc.selectNodes("//book/Title[contains(.,'localhost')]")
MsgBox "Matching Nodes : " & nlist.length

For Each node In nlist
  WScript.Echo node.nodeName & " : " & node.Text
Next

Another way to do it would be a rather dirty way, but it would work. You can perform a simple string replace, replacing ">localhost<" with ">manager<". by including the ">" and "<" characters, it would ensure the XML node value was exactly "localhost".

strXML = "<foo><bar>localhost</bar><bar2>localhost</bar></foo>"
WScript.echo Replace(strXML, ">localhost<", ">manager<")

3 Comments

How easy is this to turn into a batch script?
I already wrote a similar script to this in Visual Basic, but I can't seem to be able to run it on remote computers with psexec. This may be possible and I wrote a question on it here, but I figured I might as well try this approach as well.
Using batch files, you might be able to use dostips.com/DtTipsStringManipulation.php#Snippets.Replace
0

I was able to get this to work by using the vbscript solutions provided. The reasons I hadn't committed to a Visual Basic script before was that I didn't think it was possible to execute this script remotely with PsExec. It turns out I solved this problem as well with the help of Server Fault. In case you are interested in how that works, cscript.exe is the command parameter of PsExec and the vbscript file serves as the argument of cscript. Thanks for all the help, everyone!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.