Thursday, 25 December 2014

How to update XML data from QTP.


'How to rename the title of first book
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)

' update the attribute of the second book
Set node = xmlDoc.SelectSingleNode("/bookstore/book[1]/title/@published")
node.Text = "2009"

' 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)

' 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)

-------------XML file content ------------------

<?xml version="1.0"?>
<bookstore>
    <book>
        <title published="1595">Romeo and Juliet</title>
        <author>William Shakespeare</author>
    </book>
    <book>
        <title published="1997">Yet Another Book</title>
        <author>John Smith</author>
    </book>
    <book>
        <title published="2008">Absolute Knowledge</title>
        <author>John Smith</author>
        <author>Mark Coverdy</author>
    </book>
</bookstore>

No comments:

Post a Comment