0

First of all thanks for this great utility "XMLUnit".

I have a requirement where I want to compare 2 XML's and find the differences. Once the differences are found, I need to generate an excel report which will display the whole XML data (similar data as well as different data). The data similarities and differences will be highlighted in this excel using some color.

Example-

First XML-

    <Project>
    <Policy>
        <Amount>20</Amount>
        <Balance>10</Balance>
        <PolicyDetails>
            <Amount>30</Amount>
            <SendDate>2017-04-03</SendDate>
        </PolicyDetails>
        <PolicyDetails>
            <Amount>100</Amount>
            <SendDate>2017-04-02</SendDate>
        </PolicyDetails>
        <PolicyDetails>
            <Amount>50</Amount>
            <SendDate>2016-04-02</SendDate>
        </PolicyDetails>
    </Policy>
</Project>

Second XML -

    <Project>
    <Policy>
        <Amount>10</Amount>
        <Balance>10</Balance>
        <PolicyDetails>
            <Amount>50</Amount>
            <SendDate>2017-04-03</SendDate>
        </PolicyDetails>
        <PolicyDetails>
            <Amount>100</Amount>
            <SendDate>2017-05-05</SendDate>
        </PolicyDetails>
    </Policy>
</Project>

Now I need to find the similarities and differences of these XML's and generate an excel which will display data as-

enter image description here

I know I can read the XML's using JAVA one by one and do this but the actual XML's will have different combinations of data where ordering can be different, etc.

What I have done so far- I have used XMLUnit 2 "DiffBuilder" and found all the differences in these XML's. I have even used my custom DifferenceEvaluator for some custom logic.

I want to figure out how do I write the differences and similarities in the desired format in excel. Can anyone please help me here?

I also thought of implementing ComparisonListener and catch hold of every similarity and difference and start writing data from here in excel.

Ankit
  • 900
  • 7
  • 9
  • That's broad, please narrow down to some specific issue –  Oct 12 '17 at 15:37
  • @RC. sorry for delay. I am using XMLUnit 2.0 and I need help for following issues- 1. Once XMLUnit returns me the differences, how do i read them and insert in excel. The differences returned by XMLUnit is not that human readable. 2. Is there a way to identify the similarities in XML's using XMLUnit 2.0 – Ankit Oct 13 '17 at 15:25

1 Answers1

0

I've done something like this by turning each xml into a Map<String, String> where the map key is the xpath.

Eg for your first XML I'd create a Map like:

{
    "Project[1]/Policy[1]/Amount[1]": "20",
    "Project[1]/Policy[1]/Balance[1]": "10",
    "Project[1]/Policy[1]/PolicyDetails[1]/Amount[1]": "30",
    "Project[1]/Policy[1]/PolicyDetails[1]/SendDate[1]": "2017-04-03",
    "Project[1]/Policy[1]/PolicyDetails[2]/Amount[1]": "100",
    "Project[1]/Policy[1]/PolicyDetails[2]/SendDate[1]": "2017-04-02",
    // etc
}

You can then compare the two maps for missing/different entries. You can use the xpaths to provide a nice UI where clicking on the cell links to the location in the XML.

lance-java
  • 25,497
  • 4
  • 59
  • 101