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

QTP DotNetFactory Part 8

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.

  1. <?xml version="1.0"?>
  2. <catalog>
  3. <book id="bk101">
  4. <author>Gambardella, Matthew</author>
  5. <title>XML Developer's Guide</title>
  6. <genre>Computer</genre>
  7. <price>44.95</price>
  8. <publish_date>2000-10-01</publish_date>
  9. <description>An in-depth look at creating applications with XML.</description>
  10. </book>
  11.  
  12. <book id="bk102">
  13. <author>Ralls, Kim</author>
  14. <title>Midnight Rain</title>
  15. <genre>Fantasy</genre>
  16. <price>5.95</price>
  17. <publish_date>2000-12-16</publish_date>
  18. <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
  19. </book>
  20.  
  21. <book id="bk103">
  22. <author>Corets, Eva</author>
  23. <title>Maeve Ascendant</title>
  24. <genre>Fantasy</genre>
  25. <price>5.95</price>
  26. <publish_date>2000-11-17</publish_date>
  27. <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
  28. </book>
  29. </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.

  1. Set oXML=MyXMLReader.Create("C: \Books.xml")
  2. 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

  1. Set MyXMLReader = DotNetFactory.CreateInstance("System.Xml.XmlReader", "System.Xml")
  2. Set oXML=MyXMLReader.Create("D:\My QTP\DNFXML\Books.xml")
  3.  
  4. while (oXML.Read())
  5. If oXML.NodeType = "XmlDeclaration" or oXML.NodeType = "Element"Then
  6. print "NodeType:"&oXML.NodeType &" Name :"& oXML.Name '& " is at Depth:"& oXML.Depth
  7. If oXML.HasValue Then
  8. Print " Value: "& oXML.Value
  9. End If
  10. End if
  11. wend
  12.  
  13. oXML.Close()
  14. Set oXML = Nothing
  15. Set MyXMLReader = Nothing

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

XML Node Types

 

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

  1. Set MyXMLReader = DotNetFactory.CreateInstance("System.Xml.XmlReader", "System.Xml")
  2. Set oXML=MyXMLReader.Create("D:\My QTP\DNFXML\Books.xml")
  3.  
  4. while (oXML.Read())
  5. If oXML.NodeType = "XmlDeclaration" or oXML.NodeType = "Element"Then
  6. print "NodeType:"&oXML.NodeType &" Name :"& oXML.Name '& " is at Depth:"& oXML.Depth
  7. If oXML.HasValue Then
  8. Print " Value: "& oXML.Value
  9. End If
  10.  
  11. print "No. of Attributes : " & oXML.AttributeCount
  12.  
  13. If oXML.AttributeCount > 0 Then
  14. While oXML.MoveToNextAttribute()
  15. LocalName = oXML.Name
  16. NURI = oXML.NamespaceURI
  17. Print "Attribute : " & LocalName & " Value : " & oXML.GetAttribute(LocalName,NURI)
  18. wend
  19. end if
  20. end if
  21. wend
  22.  
  23. oXML.Close()
  24. Set oXML = Nothing
  25. Set MyXMLReader = Nothing

This will return you the elements with attribute values.

Node Types and 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

  1. Set MyXMLReader = DotNetFactory.CreateInstance("System.Xml.XmlReader", "System.Xml")
  2. Set oXML=MyXMLReader.Create("C: \Books.xml")
  3.  
  4. while (oXML.Read())
  5. If oXML.NodeType = "XmlDeclaration" or oXML.NodeType = "Element"Then
  6. print "NodeType:"&oXML.NodeType &" Name :"& oXML.Name '& " is at Depth:"& oXML.Depth
  7. If oXML.HasValue Then
  8. Print " Value: "& oXML.Value
  9. End If
  10.  
  11. If oXML.NodeType = "Element" Then
  12. Print "String Value : " & oXML.ReadString
  13. End If
  14.  
  15. If oXML.AttributeCount > 0 Then
  16. While oXML.MoveToNextAttribute()
  17. LocalName = oXML.Name
  18.  
  19. NURI = oXML.NamespaceURI
  20. Print "Attribute : " & LocalName & " Value : " & oXML.GetAttribute(LocalName,NURI)
  21. wend
  22. end if
  23. end if
  24. wend
  25.  
  26. oXML.Close()
  27. Set oXML = Nothing
  28. 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.

  1. 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

  1. Set MyXMLWriter = DotNetFactory.CreateInstance("System.Xml.XmlWriter", "System.Xml")
  2. Set oXml=MyXMLWriter.Create("C:\Test.xml")
  3. oXml.WriteStartElement("Catalog")
  4. oXml.WriteStartElement("Book")
  5. oXml.WriteAttributeString "id", "bk101"
  6. oXml.WriteStartElement("Author")oXml.WriteString "Gambardella, Matthew"
  7. oXml.WriteEndElement()
  8. oXml.WriteStartElement("Title")
  9. oXml.WriteString "XML Developer's Guide"
  10. oXml.WriteEndElement()
  11. oXml.WriteStartElement("Genere")
  12. oXml.WriteString "Computer"
  13. oXml.WriteEndElement()
  14. oXml.WriteStartElement("Price")
  15. oXml.WriteString "44.95"
  16. oXml.WriteEndElement()
  17. oXml.WriteStartElement("Publish_Date")
  18. oXml.WriteString "2000-10-01"
  19. oXml.WriteEndElement()
  20. oXml.WriteStartElement("Description")
  21. oXml.WriteString "An in-depth look at creating applications with XML."
  22. oXml.WriteEndElement()
  23. oXml.WriteEndElement()
  24. oXml.WriteEndElement()
  25. oXml.close()
  26.  
  27. Set oXML = Nothing
  28. Set MyXMLWriter = Nothing

This will return you the required xml

XML DotnetFactory

 

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.