How to update XML file from QTP

How to update XML file from QTP

In previous QTP tutorial I shown how QTP can read data from XML file.
Today I will explain how to update XML data from QTP.

We will use this XML file:
Initial XML fileNote: You can download this XML file here.

Let's check how to update different values in above XML file:
  1. How to rename the title of first book?
    The QTP script is:
    Const XMLDataFile = "C:\TestData.xml"
    Const XMLNewFile = "C:\TestData2.xml"

    Set xmlDoc = CreateObject("Microsoft.XMLDOM")
    xmlDoc.Async = False
    xmlDoc.Load(XMLDataFile)


    ' update the title of the first book
    Set node = xmlDoc.SelectSingleNode("/bookstore/book[0]/title")
    node.Text = "Romeo and Juliet - Salvation"

    ' save changes

    xmlDoc.Save(XMLNewFile)
    And the result is:
    Note: The numeration begins from zero. That's why I use book[0] to access first item.


  2. How to change the year of second book?
    I skip the opening and saving of XML file (see above QTP script). I show only the essence:
    ' update the attribute of the second book
    Set node = xmlDoc.SelectSingleNode("/bookstore/book[1]/title/@published")
    node.Text = "2009"

    And the result is:
    Note: Use @ to access an attribute of XML node.


  3. How to add new author add its new attribute?
    QTP script:
    ' select a parent node
    Set parentNode = xmlDoc.SelectSingleNode("/bookstore/book[2]")

    ' add a new author
    Set newNode = xmlDoc.CreateElement("author")
    newNode.Text = "Mr. Noname"
    parentNode.AppendChild (newNode)
    And the result is:
    As you can see, we've added "Mr. Noname" as the new author.


  4. How to add new attribute for author (XML node)?
    QTP script:
    ' select a parent node
    Set parentNode = xmlDoc.SelectSingleNode("/bookstore/book[2]")

    ' add its attribute
    Set newAttrib = xmlDoc.CreateAttribute("bestseller")
    newAttrib.Text = "yes"
    parentNode.Attributes.SetNamedItem(newAttrib)
    The result is:
    New attribute of a boof and its value ("bestseller"="yes") have been added.

Well, the working with XML files from QTP is easy enough.


Summary:
  • shown in QTP - how to change value of XML node
  • shown in QTP - how to change value of attribute
  • shown in QTP - how to add new XML node
  • shown in QTP - how to add new attribute


Do you have any comments, dear reader? Please, leave them below the present article.
Thank you in advance :)



Related articles:


Do you like this QTP tutorial? Would you like to receive them in the future?
If yes, please subscribe to this blog RSS feed or by Email. (How to subscribe? VIDEO guide)




Do you know that you are free to use/copy/publish all my materials on your site/blog?


20 comments:

Jerryzhang said...

It's very useful!
How to append a new node as one copy of an existed node ?

Dmitry Motevich said...

@ Jerryzhang,
Add new XML node and assign a required value (from an initial existed node).

Jerryzhang said...

Thanks for your reply.
I found that "xmlDocumentNode.cloneNode(true)" method can copy existed node ,and it's childnodes.

Dmitry Motevich said...

@ Jerryzhang,
Thank you.
I didn't know this method.

Jerryzhang said...

Other problem: If I saved the xml file from FireFox, the XMLDOM cannot parse it. The value returned by "xmlDoc.load()" method is False.

Dmitry Motevich said...

Check whether XML-file is correct.
P.S. No detailed info - no precise answer.

Anonymous said...

hi,
I tried the above example ...it gives me an error object required at the "node.Text = "Romeo and Juliet - Salvation" .

Dmitry Motevich said...

@Anonymous (October 8),
hi,
I tried the above example ...it does not give me an error object required at the "node.Text = "Romeo and Juliet - Salvation" .

Pavan Chennam said...

Hi this is Pavan Chennam
Regarding to print all the child nodes of a parent node how we have to proceed further can you explain?

thanks in advance

Pavan Chennam said...

hi this Pavan Chennam
I have one problem with how to get all child nodes of parent in xml using qtp? please explain

Dmitry Motevich said...

@Pavan Chennam,
/bookstore/*

Sonny said...

Hi,
I am using QTP to record SAP Portal, I am having hard time with qtp being able to record or recognize time entry cells in CAT2 SAP Tcode, any help appreciated!
also is there any way i can ask the qtp to go to next column everytime i run the test?

Dmitry Motevich said...

@Sonny,
What does it mean - "any help appreciated"?
Sorry, but I do not plan to do your work and to write QTP scripts instead of you...

Anonymous said...

Hello dmitry,

I really liked your Loadrunner video tutorials.It was of great help.Thanks a lot!
Can you guide me as to how do I go about learning Loadrunner?
Can please give us a step by step procedure to become a loadrunner expert that would be great :)

Pavan Chennam said...

hi motevich
thanks for your reply iam greatful to you for helping me in resolving my queries in Qtp and i have one query in my mind i.e how can we modify a node name can u plz suggest me
thanks in advance

Anonymous said...

Hi I tried your example.while running it is giving error saying that object required:'node'. could you please tell me how to do that.
(I downloaded your xml file, then I write the eg. in qtp test. This is what I did.)
Thank you in advance

Dmitry Motevich said...

@Anonymous (January 28, 2009),
I didn't face such issue.
So, I'm afraid I cannot help you.

Giri said...

Hi,
I have used the below mentioned example to automate my application. But I was stuck up @ parameterizing the "Book[2]" When accessing nodes i want to use generalize it instead of mentioning 2 or 3 in braces. When i try to pass as parameter it is not reading any elements with in the node. Please help me in solving the issue.
' select a parent node
Set parentNode = xmlDoc.SelectSingleNode("/bookstore/book[2]")

' add its attribute
Set newAttrib = xmlDoc.CreateAttribute("bestseller")
newAttrib.Text = "yes"
parentNode.Attributes.SetNamedItem(newAttrib)

Anonymous said...

I tried the code mentioned above to update a node in XML. I get "Object Required" Error.
Any help would be greatly appreciated!!
Thanks

Anonymous said...

It's a very useful information; clear and concise explanation; easy to understand.

Thank you for sharing.