Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QTP file compare - ignoring file structure
#1
Not Solved
Hi all,

I am looking for a generic way to compare the information in 2 separate (xml) files regardless of the structure of the files.

Example and script below. Although the information in the files are equal the test results in a failure.

================
EXAMPLE:

****** Test_1.xml ******

Code:
<note>
  <to>John</to>
  <from>Sue</from>
  <heading>Reminder</heading>
  <body>Don't forget the meeting!</body>
  </note>

****** Test_2.xml ******

Code:
<note>
  <to>John</to>
  <from>Sue</from>
  <body>Don't forget the meeting!</body>
  <heading>Reminder</heading>
  </note>

================

Script I am using only does a 1-2-1 compare.
Any help is appreciated.



================
SCRIPT:

Code:
Set xml_Doc1 = CreateObject("Msxml2.DOMDocument")
xml_Doc1.load("C:\Test\QTP\Test_1.xml")

Set xml_Doc2 = CreateObject("Msxml2.DOMDocument")
xml_Doc2.load("C:\Test\QTP\Test_2.xml")

Set Elem_File_1= xml_Doc1.DocumentElement.ChildNodes
Set Elem_File_2= xml_Doc2.DocumentElement.ChildNodes


If Elem_File_1.length = Elem_File_2.length Then
Reporter.ReportEvent Pass,"same number of Child nodes?", "File 1 & File 2 have same number of Child nodes"
Else
Reporter.ReportEvent Fail,"same number of Child nodes?",  "File 1 & File 2 have different Child nodes"
WScript.Quit
end if

For i = 0 to Elem_File_1.length-1

If Elem_File_1.item(i).Text = Elem_File_2.item(i).Text Then
Elem_File_1.item(i).Text & vbnewline &  "In File-2, The value is: "&Elem_File_2.item(i).Text
Reporter.ReportEvent Pass,"same nodes?", "File 1 & File 2 have same number of Child nodes"
Else
Reporter.ReportEvent Fail,"same nodes?", "Child Element: "& i &" is NOT SAME in File-1 & File-2" & vbnewline & "In File-1, The value is: " & Elem_File_1.item(i).Text & vbnewline & "In File-2, The value is: "&Elem_File_2.item(i).Text
End If

Next

================
Reply
#2
Not Solved
Probably the space. Use the trim to see if it helps.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#3
Not Solved
Hello Basanth,

Thanks for your reply.

While Trim would help remove the whitespaces from the file, it does not suit my purpose. I have tags <body> and <heading> in different places, that is what is causing the failure.

Fede
Reply
#4
Not Solved
I have coded in vbscript but the msgbox can be changed to Reporter.ReportEvent without problems.

The following code is being added to your code above:
Code:
set odic1 = CreateObject("Scripting.Dictionary")
set odic2 = CreateObject("Scripting.Dictionary")

''// the key will contain the nodename and the value
''// this way we catch a "to Sue" as different from a "to John"
''// if there are still duplicates, might want to make use of the improved dictionary
' http://www.advancedqtp.com/wp-content/uploads/WLW/Animproveddictionaryobject_10D37/Dictionary.VBS
' http://www.advancedqtp.com/2008/06/an-improved-dictionary-object/

''// I have not thought about a book catalog and how to handle where the same author will occur more than once.  Increase the key to include the year? Do not know.

For i = 0 to Elem_File_1.length-1
    odic1.Add Elem_File_1.item(i).nodeName & ":" & Elem_File_1.item(i).text, 1
    odic2.add Elem_File_2.item(i).nodeName&":"&Elem_File_2.item(i).text, 1    
next

key1arr = odic1.keys
for j = 0 to uBound(key1Arr)
    if NOT(odic2.exists(key1Arr(j))) then
        msgbox "This does not exist in Test_2.xml : " & key1arr(j)
    Else
        odic2(key1arr(j)) = odic2(key1arr(j)) + 1 ' value does exist in Test_2.xml
    end if
next

s = ""
key2arr = odic2.keys
for k = 0 to uBound(key2arr)
    if odic2(key2arr(k)) = 1 Then
        s = s & vbcrlf & key2arr(k)  ' this value did not exist in Test_1.xml
    End If
next

if s <> "" Then
    s = "the following does not exist in Test_1.xml file " & vbcrlf & s
    msgbox s
else
    msgbox "The two files have the same elements."
End If

HTH,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to update CSV File and need to Save same CSV File kotaramamohana 1 3,570 10-24-2015, 08:40 AM
Last Post: tigerliew
  Comapre txt file with different number of lines anushreebehura 0 1,766 06-02-2015, 06:28 PM
Last Post: anushreebehura
  How to read a text file using FSO from bottom to up order?? vallalarasu.p 0 2,530 10-13-2014, 11:14 AM
Last Post: vallalarasu.p
  How to find a file after downloading UFT_Newbie 1 2,997 09-18-2014, 08:43 PM
Last Post: UFT_Newbie
Question Is Reporter ignoring the status of steps? dlaureano 3 3,711 06-05-2014, 01:38 PM
Last Post: Jay

Forum Jump:


Users browsing this thread: 1 Guest(s)