4

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.

1 Answer 1

10

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")
Sign up to request clarification or add additional context in comments.

1 Comment

-0.5 for using the .save method (a plain Sub) as a function that returns a string.

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.