Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript Find a node in XML node and replace the value

Tags:

vbscript

How to write a vbscript which should search for a specific node in a XML file and replace the value of that node with another value?

So far I can read a node and get the value.

set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async = "false"
objXML.load("E:\sage2\test.xml")
Set Root = objXML.documentElement

For Each x In Root.childNodes

 if x.nodename="showList" then
    plot=x.text
    msgbox plot
 end if
Next

please suggest me some example which should read a specific node in the xml file and replace the value of that node.

like image 767
S.. Avatar asked Dec 27 '22 12:12

S..


1 Answers

Here is a simple XML edit and save example in VBScript. I recommend lokking into using Xpath to select your node instead of looping over the child nodes, you can provide your XML for more detailed answer.

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.load "MYFILE.xml"

'Locate the desired node
'Note the use of XPATH instead of looping over all the child nodes
Set nNode = xmlDoc.selectsinglenode ("//parentnode/targetnode")

'Set the node text with the new value
nNode.text = "NEW VALUE"

'Save the xml document with the new settings.
strResult = xmldoc.save("MYFILE.xml")
like image 185
Anas Karkoukli Avatar answered Jan 13 '23 18:01

Anas Karkoukli