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.
5 Answers
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.
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
[text()='localhost'].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
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
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!
pythona requirement? If you run on *X, you can simply usesed. Indeed, it can be done using python.