1

I want to compare two XML files using XMLUnit (I don't want to reinvent something which is already present).

XML 1:

<?xml version="1.0"?>
<Product>
    <Property>
        <Container value="1">Test 01</Container>
        <Container value="3">Test 02</Container>
        <Container value="5">Test 03</Container>
    </Property>
</Product>

XML2:

<?xml version="1.0"?>
<Product>
    <Property>
        <Container value="3">Test 01</Container>
        <Container value="7">Test 02</Container>
        <Container value="1">Test 03</Container>
        <Container value="5">Test 04</Container>
    </Property>
</Product>

I want to compare the elements only if the node along with the attribute matches. Also if the position is different then it should be similar.

I have tried with DetailedDiff but it is showing a lot of results but I only want to extract specific changes. Please give your seggestions.

Sumit Sahoo
  • 2,949
  • 1
  • 25
  • 37
  • This may help u http://stackoverflow.com/questions/141993/best-way-to-compare-2-xml-documents-in-java – Naren Jan 31 '14 at 13:11

1 Answers1

1

If you're after an order-independent comparison then ElementQualifier would help:

http://xmlunit.sourceforge.net/userguide/html/ar01s03.html

In some cases the order of elements in two pieces of XML may not be significant. If this is true, the DifferenceEngine needs help to determine which Elements to compare. This is the job of an ElementQualifier (see Section 3.4, “ElementQualifier”).

Specifically, ElementNameAndAttributeQualifier seems to match your requirements:

Only Elements with the same name - and Namespace URI if present - as well as the same values for all attributes given in ElementNameAndAttributeQualifier's constructor qualify.

Joe
  • 29,416
  • 12
  • 68
  • 88