Quick test professional

QTP Tips QTP codes QTP Faqs and more

Merge 2 XML using Dotnet Factory

Dim objReader1,objReader2,objds1,objds2
Set objReader1 = DotNetFactory.CreateInstance(“System.Xml.XmlTextReader”, “System.Xml”,”C:\Documents and Settings\kmohankumar\Desktop\11.xml”)’ Creates the instance for the XMLWriter
Set objReader2 = DotNetFactory.CreateInstance(“System.Xml.XmlTextReader”, “System.Xml”,”C:\Documents and Settings\kmohankumar\Desktop\12.xml”)’
Set objds1 = DotNetFactory.CreateInstance(“System.Data.DataSet”, “System.Data”)
Set objds2 = DotNetFactory.CreateInstance(“System.Data.DataSet”, “System.Data”)
objds1.WriteXml(“C:\Documents and Settings\kmohankumar\Desktop\13.xml”)
Set objReader1=Nothing
Set objReader2=Nothing
Set objds1=nothing
Set objds2=nothing

Author:Mohan Kakarla

November 27, 2008 Posted by | DotNetFactory, XML Scripting | 1 Comment

Create XML using Dotnet Factory

 Dim objWriter,XmlWriter
   Set objWriter = DotNetFactory.CreateInstance(“System.Xml.XmlWriter”, “System.Xml”)’ Creates the instance for the XMLWriter
 set XmlWriter=objWriter.Create(“C:\Documents and Settings\kmohankumar\Desktop\1234.xml”)’ creates xml document
 XmlWriter.WriteStartElement(“Books”)’ start writing the element

XmlWriter.WriteAttributeString “Name”, “Mohan”‘ writes  attributes and its values to the element
 XmlWriter.WriteEndElement()’ ends writing the element

XmlWriter.WriteAttributeString “Title1”, “QTP”‘


Set objWriter =Nothing

Author:Mohan kakarla

November 27, 2008 Posted by | DotNetFactory, XML Scripting | 3 Comments

Read XML using Dotnet Factory

Set objReader = DotNetFactory.CreateInstance(“System.Xml.XmlReader”, “System.Xml”)
set XmlReader=objReader.Create(“C:\Documents and Settings\kmohankumar\Desktop\3.xml”)

while (XmlReader.Read())

If  XmlReader.NodeType = “XmlDeclaration” or XmlReader.NodeType = “Element”Then
    Reporter.ReportEvent micPass, “NodeType:”&XmlReader.NodeType &”  Name :”& XmlReader.Name& ” is at Depth:”& XmlReader.Depth,””

    If XmlReader.HasValue Then
        Reporter.ReportEvent micPass, ” Value: “& XmlReader.Value,””
    End If

    Reporter.ReportEvent micPass, “Element:”& XmlReader.Name&”  has attribute(s)”&”:”& XmlReader.AttributeCount,””

  If XmlReader.AttributeCount > 0 Then

        while  XmlReader.MoveToNextAttribute()
     fn = XmlReader.Name
     ns = XmlReader.NamespaceURI
      Reporter.ReportEvent micPass,” Attribute:”&fn &”  Value: “& ns&” Attribute:”& XmlReader.GetAttribute(fn, ns),””
     End If
End If

Author: Mohan Kakarla

November 27, 2008 Posted by | DotNetFactory, XML Scripting | 3 Comments

Compare 2 XML files

This Example was written for comparing two XML files assuming the contents of the both XML has the following elements:

Dim description, filepath
Set xmlDoc1 = CreateObject(“Msxml2.DOMDocument”)
xmlDoc1.load(“C:Documents and Settingsmohan.kakarlaDesktop1.xml”)’file 1
Set xmlDoc2 = CreateObject(“Msxml2.DOMDocument”)
xmlDoc2.load(“C:Documents and Settingsmohan.kakarlaDesktop2.xml”)’file 2
Set ElemList1= xmlDoc1.DocumentElement.ChildNodes
Set ElemList2= xmlDoc2.DocumentElement.ChildNodes
If ElemList1.length=ElemList2.length Then’ check weather both xml file has same number of childnodes
  msgbox “Both XML files have same number of Child nodes”

   For i = 0 to ElemList1.length-1

       If ElemList1.item(i).Text=ElemList2.item(i).Text Then
          msgbox “child element:”&i &” is same in both XML files”
         msgbox “child element:”& i &” is not same in both XML files, In XML file 1, The valueis:”&ElemList1.item(i).Text &” and In XML file 1, The value

     End If
End If

Author: Mohan Kumar Kakarla

March 3, 2008 Posted by | XML Scripting | , , , , , , , , , , | 111 Comments

VBScript to read XML

Dim description, filepath

VBScript to read XML  


Set xmlDoc = CreateObject(“Msxml2.DOMDocument”)

Set ElemList = xmlDoc.getElementsByTagName(“segment”)
filepath = ElemList.item(0).getAttribute(“filePath”)
MsgBox filepath

Set ElemList = xmlDoc.getElementsByTagName(“description”)
plot = ElemList.item(0).Text
MsgBox plot

  Source: Mercury Forum’s KB articles

February 14, 2008 Posted by | XML Scripting | 9 Comments

How Get Root Element attribute’s value from XML file

How Get Root Element attribute’s value from XML file

Function GetRootElementAttributeValueFromXML(sFileNameWithPath,sAttribute)
    ‘ Why we need to ADD 2 because each attribute followed by (=) and (“)
            iLenOfValue=len(sAttribute) + 2
            Set doc = XMLUtil.CreateXML()
            doc.LoadFile sFileNameWithPath
            Set root = doc.GetRootElement()
            If instr(1,root,sAttribute)  <= 0 then
                        Reporter.ReportEvent micFail,sAttribute,”Not Found in XML file.”
                        sStartPos=instr(1,root,sAttribute) + iLenOfValue
                        GetRootElementAttributeValueFromXML=mid(root,sStartPos,sEndPos – sStartPos)
            end if
End Function

February 14, 2008 Posted by | XML Scripting | 3 Comments