-2

I have the following XML file:

<TOOLING>
  <TOOLENVIRONMENT>         
    <TOOL>
      <TOOLSNAME>MyABCVersion1</TOOLSNAME>
      <TOOLSVERSION>12.34.56</TOOLSVERSION>
    </TOOL>  

    <TOOL>
      <TOOLSNAME>MyABCVersion2</TOOLSNAME>
      <TOOLSVERSION>23.45.67</TOOLSVERSION>
    </TOOL> 

    <TOOL>
      <TOOLSNAME>MyABCVersion3</TOOLSNAME>
      <TOOLSVERSION>34.56.78</TOOLSVERSION>
    </TOOL>                      
  </TOOLENVIRONMENT>
</TOOLING>

I wanted to get the value of <TOOLS_VERSION> from XML file by using the <TOOLS_NAME>. Which means I want to search in XML file for "MyABCVersion1" and retrieve the value "12.34.56". After retrieving I wanted to replace the version name with the new version name which will be provided in the parameters.

I have tried the script like the following by taking the first tag of the file, but in future if the order changes that will not work:

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.Async = False
objXMLDoc.Load("autorun.xml")
WScript.Echo objXMLDoc.Xml

Set ElemList = objXMLDoc.GetElementsByTagName("TOOLSNAME")
plot = ElemList.item(0).Text
WScript.Echo plot

Set ElemList = objXMLDoc.getElementsByTagName("TOOLSNAME")
plot = ElemList.Item(1).Text
WScript.Echo plot
Kivi
  • 485
  • 1
  • 9
  • 26

1 Answers1

0

What you want is best done with an XPath expression. Also, Microsoft.XMLDOM is deprecated. Use Msxml2.DOMDocument instead.

Set objXMLDoc = CreateObject("Msxml2.DOMDocument.6.0")
objXMLDoc.Async = False
objXMLDoc.Load("autorun.xml")
xpath = "//TOOL[./TOOLS_NAME/text()='MyABCVersion1']/TOOLS_VERSION"
Set node = objXMLDoc.SelectSingleNode(xpath)
node.Text = WScript.Arguments.Item(0)
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks a lot Ansgar, it worked for me. I wanted to replace the version name with new version name which will be provided in the parameters. For that I have used the syntax: Replace(strText, "OldValue", Wscript.Arguments.Item(0)). But it does not work for me. How can I replace here ? – Kivi Oct 09 '18 at 08:26
  • `Replace` doesn't work in-place. You need to assign the new or modified value back to the node. – Ansgar Wiechers Oct 09 '18 at 08:31
  • If I need to assign the new value to the node, how can I perform that.? Because I am bit confused.,Can you please clarify. – Kivi Oct 10 '18 at 05:12
  • I already did. Please re-read my answer. – Ansgar Wiechers Oct 10 '18 at 08:01
  • Sorry. Thank you so much. It works for me. – Kivi Oct 10 '18 at 10:11
  • @Dinesh so you're not [Manikandan R C](https://stackoverflow.com/users/10411356/manikandan-r-c) the [question](https://stackoverflow.com/q/52735729/692942) looked pretty similar until you deleted it. – user692942 Oct 10 '18 at 11:26
  • I am not getting you, and I am not that person. – Kivi Oct 11 '18 at 06:29