.

Part8: QTP and DotNetfactory – UsingXML

This is Part8 and last in series  of QTP and DotNetFactory series by Saket. Refer Part1, Part2,Part3, Part4, Part5, Part6, Part7

qtp-dotnetfactory-part8

XML stands for Extensible Markup Language. It a markup language very much similar to HTML but not with predefined tags, used for encoding documents electronically. It has become a very popular means to exchange structured data.

There are several methods in QTP to work with XML file like using XMLUtil, and MSXML. In this article you will learn how to interact with XML files using DotNetFactory in QTP.

Here is an excerpt of sample XML file (books.xml) from msdn. This XML will be used for all the examples in this article.

<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>

<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>

<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
</book>
</catalog>

To work with using DotNetFactory in QTP we will use ‘System.XML’ namespace, which provides standards-based support for processing xml and we can use different classes for different operations like XMLDocument, XMLTextReader, XMLTextWriter etc.

Let us now see how we can do various operations with xml.

Reading a XML document

To Read the XML file we will use ‘XMLReader’ class, which provide a read only access to a stream of XML data. We will use ‘System.XML.XMLReader’ as type and ‘System.XML’ as assembly.

Set MyXMLReader = DotNetFactory.CreateInstance ("System.XML.XMLReader", "System.XML")

To read a particular xml file, we can use ‘Create’ method which creates a new instance of XMLReader with the specified XML file. Once the xml is initialized we can use ‘Read’ method to read the first node.

Set oXML=MyXMLReader.Create("C: \Books.xml")
oXML.Read

XML Documents always form a tree structure starting with a root and branches to leaves. In the above sample xml document the first line the XML declaration which defines the XML version.

XML Declaration will always have the node type as ‘XMLDeclaration’ and root and other leaves will have the node type as ‘Element’. Use ‘NodeType’ property to get the type of node in xml document. There are several other properties as well which we can use here like

Name – gives the qualified name of the current node.

HasValue – gives a value, whether the current node has the value.

AttributeCount – gives the number of attributes in the current node

Example 29

Set MyXMLReader = DotNetFactory.CreateInstance("System.Xml.XmlReader", "System.Xml")
Set oXML=MyXMLReader.Create("D:\My QTP\DNFXML\Books.xml")

while (oXML.Read())
If oXML.NodeType = "XmlDeclaration" or oXML.NodeType = "Element"Then
print "NodeType:"&amp;oXML.NodeType &amp;" Name :"&amp; oXML.Name '&amp; " is at Depth:"&amp; oXML.Depth
If oXML.HasValue Then
Print " Value: "&amp; oXML.Value
End If
End if
wend

oXML.Close()
Set oXML = Nothing
Set MyXMLReader = Nothing

This will return you the XML Declaration and all the elements in the xml document.

XMLNodeType

Now to get the attributes we will use ‘GetAttribute’ method, which returns the value of an attribute. It requires two parameter Node Local name and namespace uri.

oXML.GetAttribute(oXML.Name, oXML.NamespaceURI)

to move through each attribute, we can MoveToNextAttribute method.

Example 30

Set MyXMLReader = DotNetFactory.CreateInstance("System.Xml.XmlReader", "System.Xml")
Set oXML=MyXMLReader.Create("D:\My QTP\DNFXML\Books.xml")

while (oXML.Read())
If oXML.NodeType = "XmlDeclaration" or oXML.NodeType = "Element"Then
print "NodeType:"&amp;oXML.NodeType &amp;" Name :"&amp; oXML.Name '&amp; " is at Depth:"&amp; oXML.Depth
If oXML.HasValue Then
Print " Value: "&amp; oXML.Value
End If

print "No. of Attributes : " &amp; oXML.AttributeCount

If oXML.AttributeCount &gt; 0 Then
While oXML.MoveToNextAttribute()
LocalName = oXML.Name
NURI = oXML.NamespaceURI
Print "Attribute : " &amp; LocalName &amp; " Value : " &amp; oXML.GetAttribute(LocalName,NURI)
wend
end if
end if
wend

oXML.Close()
Set oXML = Nothing
Set MyXMLReader = Nothing

This will return you the elements with attribute values.

NodeType & attributes

You can see here we have got the values for book id but not for author, title, genre etc.

To get these values we use ‘ReadString’ method

Example 31

Set MyXMLReader = DotNetFactory.CreateInstance("System.Xml.XmlReader", "System.Xml")
Set oXML=MyXMLReader.Create("C: \Books.xml")

while (oXML.Read())
If oXML.NodeType = "XmlDeclaration" or oXML.NodeType = "Element"Then
print "NodeType:"&amp;oXML.NodeType &amp;" Name :"&amp; oXML.Name '&amp; " is at Depth:"&amp; oXML.Depth
If oXML.HasValue Then
Print " Value: "&amp; oXML.Value
End If

If oXML.NodeType = "Element" Then
Print "String Value : " &amp; oXML.ReadString
End If

If oXML.AttributeCount &gt; 0 Then
While oXML.MoveToNextAttribute()
LocalName = oXML.Name

NURI = oXML.NamespaceURI
Print "Attribute : " &amp; LocalName &amp; " Value : " &amp; oXML.GetAttribute(LocalName,NURI)
wend
end if
end if
wend

oXML.Close()
Set oXML = Nothing
Set MyXMLReader = Nothing

This will return you all the elements and their values

XML elements and Values

Now, as you have learnt to read a XML document, we will quickly see how to create a xml document

Create XML

For Creating XML we will use XMLWriter Class, System.XML.XMLWriter as type name and System.XML as Assembly.

Set MyXMLWriter = DotNetFactory.CreateInstance("System.Xml.XmlWriter", "System.Xml")

Same as Reader we can use ‘Create’ method which creates a new instance of XMLWriter with the specified XML file.

Set oXml=MyXMLWriter.Create("C:\Test.xml")

We use different methods to create the xml data

WriteStartElement – writes the specified start tag
WriteEndElement – writes the specified end tag
WriteAttributeString – writes an attribute with specified value
WriteElementString – wites an element with specified value

So if you need to create a XML as above document the script will be

Example 32

Set MyXMLWriter = DotNetFactory.CreateInstance("System.Xml.XmlWriter", "System.Xml")
Set oXml=MyXMLWriter.Create("C:\Test.xml")
oXml.WriteStartElement("Catalog")
oXml.WriteStartElement("Book")
oXml.WriteAttributeString "id", "bk101"
oXml.WriteStartElement("Author")oXml.WriteString "Gambardella, Matthew"
oXml.WriteEndElement()
oXml.WriteStartElement("Title")
oXml.WriteString "XML Developer's Guide"
oXml.WriteEndElement()
oXml.WriteStartElement("Genere")
oXml.WriteString "Computer"
oXml.WriteEndElement()
oXml.WriteStartElement("Price")
oXml.WriteString "44.95"
oXml.WriteEndElement()
oXml.WriteStartElement("Publish_Date")
oXml.WriteString "2000-10-01"
oXml.WriteEndElement()
oXml.WriteStartElement("Description")
oXml.WriteString "An in-depth look at creating applications with XML."
oXml.WriteEndElement()
oXml.WriteEndElement()
oXml.WriteEndElement()
oXml.close()

Set oXML = Nothing
Set MyXMLWriter = Nothing

This will return you the required xml

XMLDotnetFactory

Thank You Saket for sharing your knowledge with the QTP community. I’m sure this exhaustive series on DotNetFactory will be of immense help to all of us.

Have you downloaded the FREE Optimizing QTP eBook yet? Get It Now!

If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

Related posts:

  1. Part7: QTP and DotNetfactory – ArrayList
  2. Part6: QTP and DotNetfactory – Manipulating Date/Time
  3. Part1: QTP and DotNetFactory – Basics
  4. Part2: QTP and DotNetFactory – Creating a User Form
  5. Part5: QTP and DotNetFactory – Creating a Progress Bar Control

13 comments ↓

#1 Karthikeyan on 01.21.10 at 01:37

Some of the code examples have typo in it. VB operators like & are replaced by their XML counterparts. For example

If a > b Then

Which is supposed to be

If a>b Then

#2 Karthikeyan on 01.21.10 at 01:52

Some of the code examples have typo in it. VB operators like & are replaced by their XML counterparts. For example

If a & gt; b Then

Which is supposed to be

If a>b Then

#3 sreedhar on 01.21.10 at 10:34

If we need to create more number of branches to each element what is the procedure to do it. If you can explain that it will be of good use.

#4 Soumya on 01.26.10 at 22:29

Hi Saket,

Thanks for a fantastic article

I Have a question . How can we modify only some fields in XML ?

#5 Saket on 02.01.10 at 09:59

Yes Karthikeyan, you are right. we will definitely take care of this in future. Thanks

#6 Saket on 02.01.10 at 10:03

hi Soumya,

may be you can read nodes from the xml first and then replace your modified string wherever required.

#7 Radha on 02.16.10 at 08:52

Hi Sacket /Karthik
Can you please correct the code as mentioned by Karthikeyan and publish please.THANKS
Regards
Radha

#8 zephy on 02.24.10 at 14:30

not sure if this is where it’s supposed to go, i have this
Browser(“Emptoris Contract Management”).Window(“Search Contract Templates — Webpage Dialog”).Page(“Search Contract Templates_2″).Frame(“content”).WebElement(“WebTable”).Click

but when i execute it i get an error. any idea what’s wrong?

#9 Mahesh Kothale on 03.03.10 at 17:57

Hi ,
I am testing application having WPF (windows presention Foundation) Controls.I am using qtp 10.0 with add-ins .Net and WPF . but WPF controls does not get recognize by QTP. Can u pls tell me what are the settings are required..

Thanks
Mahesh Kothale

#10 Miken on 04.27.10 at 00:58

Using QTP 9.5 and am unable to load System.XML.XMLreader with statement Set MyXMLReader = DotNetFactory.CreateInstance (“System.XML.XMLReader”, “C:\WINDOWS\assembly\System.XML”). System.XML is present in windows assembly. any ideas why?

#11 Saket on 04.27.10 at 14:42

Hi Miken,
is there anything specific you need to put it this way?the second parameter required for createinstance is actually the namespace and it bydefault loaded, there is no need to put the complete path here. you should use this way to call an assemble which is not loaded may be in the case when you wan tot use your custom .net dll.

#12 Bruce on 05.19.10 at 19:44

Hi Saket,

Thanks for a fantastic article

I Have a question . How can we modify only some fields in XML ?

#13 Saket on 05.20.10 at 10:00

Hi Bruce,
One of the way could be to go through all the nodes in the xml and change the required and save. can you elaborate your requirement and post it at QTP Forums?

Leave a Comment