I am trying to compare two xml
files with attributes and child elements regardless of the xml
tag order. I am using XMLUnit
for same. I created a diff
using mentioned code and was expecting that it will say it's similar. But actually it's giving differences. I want to know how to compare mentioned xml
files while ignoring tag orders, as my code doesn't do that correctly.
Diff xmlDiff = DiffBuilder
.compare(expected)
.withTest(actual)
.ignoreWhitespace()
.normalizeWhitespace()
.checkForSimilar()
.withNodeMatcher(new DefaultNodeMatcher(
ElementSelectors.and(
ElementSelectors.byXPath("/root/tag", ElementSelectors.byNameAndAllAttributes),
ElementSelectors.byXPath("/root/tag/fixedValue", ElementSelectors.byNameAndText)
)
))
.build();
Source XML
<root>
<tag attr1="attr1" attr2="attr12">
<fixedValue>v1</fixedValue>
</tag>
<tag attr1="attr2" attr2="attr22">
<fixedValue>v2</fixedValue>
</tag>
</root>
Destination XML
<root>
<tag attr1="attr2" attr2="attr22">
<fixedValue>v2</fixedValue>
</tag>
<tag attr1="attr1" attr2="attr12">
<fixedValue>v1</fixedValue>
</tag>
</root>
It produces following output if I print the differences (should say 'SIMILAR' not 'DIFFERENT')
Expected attribute value 'attr1' but was 'attr2' - comparing <tag attr1="attr1"...> at /root[1]/tag[1]/@attr1 to <tag attr1="attr2"...> at /root[1]/tag[1]/@attr1 (DIFFERENT)
Expected attribute value 'attr12' but was 'attr22' - comparing <tag attr2="attr12"...> at /root[1]/tag[1]/@attr2 to <tag attr2="attr22"...> at /root[1]/tag[1]/@attr2 (DIFFERENT)
Expected text value 'v1' but was 'v2' - comparing <fixedValue ...>v1</fixedValue> at /root[1]/tag[1]/fixedValue[1]/text()[1] to <fixedValue ...>v2</fixedValue> at /root[1]/tag[1]/fixedValue[1]/text()[1] (DIFFERENT)
Expected attribute value 'attr2' but was 'attr1' - comparing <tag attr1="attr2"...> at /root[1]/tag[2]/@attr1 to <tag attr1="attr1"...> at /root[1]/tag[2]/@attr1 (DIFFERENT)
Expected attribute value 'attr22' but was 'attr12' - comparing <tag attr2="attr22"...> at /root[1]/tag[2]/@attr2 to <tag attr2="attr12"...> at /root[1]/tag[2]/@attr2 (DIFFERENT)
Expected text value 'v2' but was 'v1' - comparing <fixedValue ...>v2</fixedValue> at /root[1]/tag[2]/fixedValue[1]/text()[1] to <fixedValue ...>v1</fixedValue> at /root[1]/tag[2]/fixedValue[1]/text()[1] (DIFFERENT)