0% found this document useful (0 votes)
1K views3 pages

QTP - How To Update XML File From QTP

The document explains how to update values in an XML file from QTP. It shows how to: 1) rename the title of the first book, 2) change the year of the second book, 3) add a new author node and its attribute, and 4) add a new attribute to an existing node. The key steps are to load the XML file into an XMLDOM object, select the desired node(s) using XPath, and then update/add nodes and attributes as needed before saving the changes.

Uploaded by

api-19840982
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views3 pages

QTP - How To Update XML File From QTP

The document explains how to update values in an XML file from QTP. It shows how to: 1) rename the title of the first book, 2) change the year of the second book, 3) add a new author node and its attribute, and 4) add a new attribute to an existing node. The key steps are to load the XML file into an XMLDOM object, select the desired node(s) using XPath, and then update/add nodes and attributes as needed before saving the changes.

Uploaded by

api-19840982
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

Note: 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/@publishe
d")
node.Text = "2009"

And the result is:

Not
e: 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:

N
ew 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy