05-21-2014, 12:01 PM
Hi,
I'm quite new to QTP / UFT and I'm quite stuck with my script currently. My goal is to compare two huge XML files if they matches and print each line to Run result viewer with Passed/Failed status (if failed, then showing the difference).
I have a working script, but I have probably the wrong approach to the problem:
'-------------------------------------
'-------------------------------------
Any advices how I could compare the XML files?
I'm quite new to QTP / UFT and I'm quite stuck with my script currently. My goal is to compare two huge XML files if they matches and print each line to Run result viewer with Passed/Failed status (if failed, then showing the difference).
I have a working script, but I have probably the wrong approach to the problem:
'-------------------------------------
Code:
For i = 1 To 100
DataTable.Import"C:\test1.xml"
File1 = DataTable.GetSheet(1).GetParameter("A").ValueByRow(i)
DataTable.Import"C:\test2.xml"
File2 = DataTable.GetSheet(1).GetParameter("A").ValueByRow(i)
If File1 = File2 Then
Reporter.ReportEvent micPass, "Comparison", "Rows are identical"
Else
Reporter.ReportEvent micFail, "Comparison", "Rows are not identical"& vbNewline &"File1 has " &File1& ". "& vbNewline & "File2 has " &File2
End If
Next
'-------------------------------------
The problem with the current script is that it is way too slow, because it is importing XML files in FOR loop.
I also found this solution and it works in some simple XML files (but still gives quite unclear results), but with the XML files I'm using it just gives "Object required: 'xmlDoc1.DocumentElement' error message when running it. Not quite sure why.
'-------------------------------------
Dim description, filepath
Set xmlDoc1 = CreateObject("Msxml2.DOMDocument")
xmlDoc1.load("C:\test1.xml")'file 1
Set xmlDoc2 = CreateObject("Msxml2.DOMDocument")
xmlDoc2.load("C:\test2.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
Reporter.ReportEvent micPass,"Child nodes", "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
Reporter.ReportEvent micPass, "child element", "child element:"&i &" is same in both XML files"
Else
Reporter.ReportEvent micFail, "child element", "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 is:"&ElemList2.item(i).Text
End If
Next
End If
'-------------------------------------
Any advices how I could compare the XML files?