Search code examples
javaxmlxmlunit

XMLUnit show added or removed nodes


I have two xml files like this:

<personlist>
    <person>
        <name>Test</name>
    </person>
    <person>
        <name>Test2</name>
    </person>
</personlist>

and this

<personlist>
    <person>
        <name>Test</name>
    </person>
</personlist>

I know want to use XMLUnit to get the difference between these two xml files. Is there a way that XMLUnit shows me what nodes are added or removed? If I compare the xml files i want a result like:

1 person node removed

Solution

  • Added and removed nodes are a special kind of difference. So you need to use one of the methods that give you all differences. If using XMLUnit 1.x that means using DetailedDiff and getAllDifferences - if using XMLUnit 2.x that means looking at Diff and getDifferences and most likely DiffBuilder.

    XMLUnit will flag a difference with either the control or the test node detail being null with type CHILD_NODE_NOT_FOUND_ID in 1.x and CHILD_LOOKUP in 2.x. If the test detail is null, the node has been removed. If the control detail is null it has been added.

    When using XMLUnit 1.x ensure to set XMLUnit.setCompareUnmatched(false) or you may not receive any CHILD_NODE_NOT_FOUND differences at all.