Powered by: Dharamveer Saxena

Wednesday, January 30, 2013

Reading and writing in XML using QTP - Vbscript


Using VbScript one can read and write into an XML file by creating an object of "Msxml2.DOMDocument"



If one needs to read or write into this XML file, then it can be done using the below procedure where I have used "object_" and "data_file_path" as parameter.
1. object_ can be "_path" if you want to get values inside "_path" tag in above XML
2. data_file_path is XML file path like "c:\data.xml"

Sub Write_read_XML(object_, data_file_path)

'creating object for "Msxml2.DOMDocument"
       Set xmlDoc = CreateObject("Msxml2.DOMDocument")

'load xml file on object
       xmlDoc.load(data_file_path)

'creating an object which can store all values in tag: like in this case all the values inside object_ tag will be saved in ElemList
       Set ElemList = xmlDoc.getElementsByTagName(object_)

'Now loop through all the tags with name object_ in your XML file
       for n = 0 to 1

      'Now to fetch value you can use:
               msgbox ElemList.item(n).text

      'Incase you need to save values in XML then you can use the code:
               ElemList.item(n).text = "value " & cstr(n)

       next
End Sub


1 comment:

  1. I am getting a "Name redefined" error for my functional library since i used Msxml2.DOMDocument. Do you know how can this be fixed?

    ReplyDelete