0

I have two xmls, that needs to be compared. Number of elements in both the xml's may vary. Say for example consider below two xmls:

XML1:

<Students>
                <Name Type="PG">
                    <GivenName>David</GivenName>
                    <Surname>John</Surname>
                </Name>
                <Name Type="PG">
                    <GivenName>Naveen</GivenName>
                    <Surname>S</Surname>
                </Name>
                <Telephone AreaCityCode="BLR">
                    <PhoneNumber No="+91-9643542222"/>
                </Telephone>
               <Telephone AreaCityCode="BLR">
                    <PhoneNumber No="+91-9643542258"/>
                </Telephone>
                <Email>David.JOHN@gmail.com</Email>
                <Email>Naveen.S@gmail.com</Email>
                <Address Type="PT">
                    <AddressLine>40 DICKENSON ROAD</AddressLine>
                    <AddressLine>BANGALORE 12</AddressLine>
                </Address>
                <Address Type="TP">
                    <AddressLine>DOUBLE ROAD</AddressLine>
                    <AddressLine>BANGALORE 20</AddressLine>
                </Address>
</Students>

XML2:

 <Students>
                <Name Type="PG">
                    <GivenName>David</GivenName>
                    <Surname>John</Surname>
                </Name>
                <Name Type="PG">
                    <GivenName>Naveen</GivenName>
                    <Surname>S</Surname>
                </Name>
                <Telephone AreaCityCode="BLR">
                    <PhoneNumber No="+91-9643542222"/>
                </Telephone>
               <Telephone AreaCityCode="BLR">
                    <PhoneNumber No="+91-9643542258"/>
                </Telephone>
                <Email>David.JOHN@gmail.com</Email>
                <Address Type="PT">
                    <AddressLine>40 DICKENSON ROAD</AddressLine>
                    <AddressLine>BANGALORE 12</AddressLine>
                </Address>
                <Address Type="TP">
                    <AddressLine>DOUBLE ROAD</AddressLine>
                    <AddressLine>BANGALORE 30</AddressLine>
                </Address>
</Students>

In XML2, number of email element is only one, but where as in XML1 it is two. My task is to compare the content of AddressLine element irrespective of number of email elements. I have written a piece of code for obtaining this as below:

XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); 
 DetailedDiff detailledDiff = new DetailedDiff (XMLUnit.compareXML(XML1, XML2));
    detailledDiff.overrideElementQualifier(new ElementNameAndTextQualifier());
    List<Difference> list = detailledDiff.getAllDifferences();
    for (Difference difference : list) {
        if(difference.getTestNodeDetail().getXpathLocation() == null)
            System.out.println("control: "+difference.getControlNodeDetail().getXpathLocation());
        if(difference.getControlNodeDetail().getXpathLocation() == null)
            System.out.println("test: "+difference.getTestNodeDetail().getXpathLocation());
    }

Even though there is a change in AddressLine, it doesn't print the addressLine XPath. Please help me out in resolving this issue. Thanks in advance

Pramod
  • 731
  • 2
  • 10
  • 24
  • I cannot reproduce your same results with your same XMLs and tester. For me, it returns just `control: /Students[1]/Email[2]`. I've tried xmlunit-1.3 and xmlunit-1.5. Check the input files you are reading. – Little Santi Jan 03 '16 at 10:05

1 Answers1

0

I guess maybe you have some whitespace (blanks, newlines, etc) too much in one of your files. To ignore whitespace, add this line at the beginning of your tester:

    XMLUnit.setIgnoreWhitespace(true);
Little Santi
  • 8,563
  • 2
  • 18
  • 46