Micro Focus QTP (UFT) Forums
Genaralised code for checking the comparision of two xml files having child nodes. - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Genaralised code for checking the comparision of two xml files having child nodes. (/Thread-Genaralised-code-for-checking-the-comparision-of-two-xml-files-having-child-nodes)



Genaralised code for checking the comparision of two xml files having child nodes. - Ganta - 11-05-2009

Please some one help me....

Code:
<Applicant>
    <ApplicantType>Applicant</ApplicantType>
    <PersonalInfo>
      <Name>
        <Prefix>DR</Prefix>
        <FirstName>Autoc</FirstName>
        <MiddleName />
        <LastName>KyacommonAL</LastName>
        <Suffix>I</Suffix>
      </Name>
      <DOB>1970-12-25</DOB>
      <SSN>123456789</SSN>
      <Gender>Male</Gender>
      <MaritalStatus>Married</MaritalStatus>
      <Industry>Art/Design/Media</Industry>
      <Occupation>Actor</Occupation>
      <YearsWithPreviousEmployer>2</YearsWithPreviousEmployer>
      <Education>Masters</Education>
      <Relation>Insured</Relation>
    </PersonalInfo>
    <Address>
      <AddressCode>StreetAddress</AddressCode>
      <Addr1>
        <StreetName>Main St.</StreetName>
        <StreetNumber>123</StreetNumber>
        <UnitNumber />
      </Addr1>
      <Addr2 />
      <City>Madison</City>
      <StateCode>AL</StateCode>
      <County>Madison</County>
      <Zip5>35757</Zip5>
      <Phone>
        <PhoneType>Home</PhoneType>
        <PhoneNumber>2145551212</PhoneNumber>
      </Phone>
      <Phone>
        <PhoneType>Work</PhoneType>
        <PhoneNumber>2145551212</PhoneNumber>
        <Extension>56</Extension>
      </Phone>
      <Email>unknown@unknown.com</Email>
      <Validation>Invalid</Validation>
    </Address>
  </Applicant>

This is the type of file i am uploading and now i need to compare the same type of files in difference in values.(Here child nodes have n number of nodes in it).

Thanks
Ramakrishna


RE: Genaralised code for checking the comparision of two xml files having child nodes. - Saket - 11-06-2009

Try the code below to compare two xml files
Code:
Set oXML1 = CreateObject("Msxml2.DOMDocument")
oXML1.load("<<Your first File Path >>")
Set oXML2 = CreateObject("Msxml2.DOMDocument")
oXML2.load("<<Your Second File Path >>")
Set XMLElements1= oXML1.DocumentElement.ChildNodes
Set XMLElements2= oXML2.DocumentElement.ChildNodes
If XMLElements1.length=XMLElements2.length Then

  msgbox "Child Nodes - Equal"

   For i = 0 to XMLElements1.length-1

       If XMLElements1.item(i).Text <> XMLElements2.item(i).Text Then
         msgbox "Elements Not same- XML files are not equal "
         Exit for
     End If
  Next
msgbox "Elements  same- XML files are equal "
else
    msgbox  "Child Nodes - Not Equal- XML files are not equal"
End If

Alternately this can be done using XMLUtils as well
Code:
xmlFile1 = "<<Your first File Path >>"
xmlFile2 = "<<Your second File Path >>"

Set oXML1 = XMLUtil.CreateXML()
Set oXML2 = XMLUtil.CreateXML()

oXML1.LoadFile (xmlFile1)
oXML2.LoadFile (xmlFile2)

CompareResult = oXML1.compare(oXML2, ResultsXML)

If CompareResult = 1 THEN
    MsgBox "XML files are equal"
Else
    MsgBox "XML files are not equal"
END IF
    ResultsXML.savefile (“c:\results.xml”)



RE: Genaralised code for checking the comparision of two xml files having child nodes. - Ganta - 11-06-2009

Saket,
Thanks for giving me immediate reply. This code is working fine for comparing of two xml files.I have used XMLUtils code.But one problem what i am facing here is In the XML file which i have uploaded may not contain some tags which is being generated when the file is exported.Now if in a Node in both the files contains same xml tags and if a new tag got generated in the exported file in the node, it is giving as State Added, this is fine but because of this the position of the tags being varied in the nodes and if same tags are present also it is showing as Stae Added.
Uploaded file:
<GoodStudent>Yes</GoodStudent>
<Student100>Yes</Student100>
<DriverTraining>No</DriverTraining>

Exported file:
<DriverTraining>No</DriverTraining>

Here in the exported file the other two tags are not generated, it is fine. But even if driver training is present also it is showing as Driver training added in the result xml.(That means the uploaded file and the exported file will not have same tags)

Please help on this.


RE: Genaralised code for checking the comparision of two xml files having child nodes. - Saket - 11-06-2009

in this case the first method should work i.e using createobject.
loop through all the elements in the first file and compare with the generated file whether the tags are there in the files or not.


RE: Genaralised code for checking the comparision of two xml files having child nodes. - Ganta - 11-18-2009

Hi Saket,
Thanks for providing me the reply.I tried using the first code, but one problem what i am facing was i need to report the Extra tags generated as well as how the invalid data uploaded is being set in the export file.
1.But from the above code i am able to see the difference in the form of message box,and the difference is being generated from node to node only with the values but not with the tags and the value.
2.If there are multiple child nodes present inside a node it is not able to identify and execute.(If i used the loop process)

From the second process itself, is there any chance to find only tags which are not present in the Export file and the value difference tags.

Thanks
Ramakrishna