QTP tutorials & interview questions


Search more on QTP here

QTP, XML DOM & VBScript

QTP, XML DOM & VBScript

XML is eXtensible Markup Language.

As intimidating as an XML document may seem, it's really nothing more than a text file that contains data wrapped in markup (tags that denote structure and meaning).

Because XML is text-based, it is not platform-dependent. That is to say, XML is not dependent on a specific application for construction, reading, or editing. This versatility promotes application interoperability, collaboration, and data sharing. In addition, because of their text-based nature, XML documents tend to compress at a higher compression rate than binary files, making them ideal for storing and archiving data.

Another benefit of XML documents is that they internally describe their own content and structure in parent/child hierarchies. This allows applications to search and extract data far more efficiently than standard text files.

Simple example of XML file:



The first line is the XML declaration. The next line describes the root element of the document: <Employee>. The next 3 lines describe 3 child elements of the root. And finally the last line defines the end of the root element:</Employee>

An element is defined by a start tag (such as <Records>) and an end tag (such as </Records>). Any data you enter between the start and end tags makes up the contents of that element. As seen below, the document begins with <Records> and ends with </Records>; all of the syntax between these tags makes up the content of the Records element:



The text Records has no predefined utility or meaning. You can change that text to anything e.g. Movies and it would be all the same to the XML document.

And this is the beauty of XML. XML allows you to create custom tags: tags to which you give definition and purpose. As long as you adhere to a few basic rules, you can create and describe any number of elements by creating your own custom tags. Here are the basic syntactic rules that must be followed when creating elements:

  • Every element must have a start tag, represented by left and right angle brackets (<>), as well as a corresponding end tag represented by a left angle bracket, forward slash, and right angle bracket (</>).
  • Names in XML are case sensitive, so the start and end tags of an element must match in case as well as in syntax. For example, an element defined by the tags <Data></data>  would cause a parsing error. XML would look for the end tag for <Data>  as well as the start tag for </data>.
  • You must begin all element names with a letter or an underscore; never a digit. In addition, names that begin with any permutation of XML are reserved and cannot be used.

Above the Records element is the root element for this particular XML document. The root element (which is always the topmost element in an XML document) serves as the container for all of the contents within the document. Every XML document must have one (and only one) root element.

Below the root element, you will see four elements, each one containing its own content. Elements can contain numbers, text, and even other elements. Elements are normally framed in a parent/child hierarchy. For instance, in the previous example, the Customer element is a child of the Records root element. Likewise, the Records element is the parent of the Customer element. Following that logic, the Quarter, Region, and Revenue elements are the children of the Customer element. This parent/child hierarchy allows the XML document to describe the structure of the data as well as the content.

Another example for a little bit more understanding:



In the above example the attribute is Class="One" and it should always be quoted, either single or double quotes.

You may also see attributes that exist in an empty element that contains no nested children. In these situations, you will see the attributes formatted as such:



Following is completely legal:

 



In the message tag < symbol is shown as "&lt;" called entity reference. There are some predefined entity references in XML.

Difference between XML Elements vs. Attributes



In the first example Home is an attribute. In the second, Home is an element.

More on XML

Extensible Markup Language (XML)
Introduction to XML


XML DOM

The XML DOM defines a standard way for accessing and manipulating XML documents. XML DOM is a programming interface for XML documents. With XML DOM, a developer can load, create, modify or delete XML information.

When using XML DOM, the first thing you need to do is to declare an object of XMLDocument type. The XMLDocument type extends the XMLNode object, which represents a node of any type in an XML document. After declaring XML Document, you need to load or create your XML document. Once you have the document loaded, you can traverse the nodes in the document by using an XMLNode object and ChildNodes property of the DocumentElement. The DocumentElement property returns back XML document and ChildNodes property returns back the collection of nodes that makes up the document.

Few resources to master XML DOM:

XML DOM - Objects, Methods, Events, Properties
XML DOM - 1

DOM Reference - XML DOM Enumerated Constants, XML DOM Objects/Interfaces, XML DOM Properties, XML DOM Methods, XML DOM Events
XML DOM - 2

XMLDOM Quick Reference guide
XML DOM - 3

Document Object Model (DOM)
XML DOM - 4

The W3C Document Object Model (DOM)
XML DOM - 5

VBScript & XML DOM

1. VBScript to access number of child nodes and text/nodename of each child node from XML file.

2. VBScript to access node name and node type of all nodes from XML document.

3. VBScript to add a new node in XML document.

4. VBScript to append a string at the end of a text node in XML file.

5. VBScript to compare two XML files and show the differences in a messagebox.

XML References:
Excel 2007 Vba: Programmer'S Reference by John Green, Stephen Bullen, Michael Ale, Rob Bovey