1

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)
Carlos Cavero
  • 3,011
  • 5
  • 21
  • 41
Kuleen
  • 11
  • 1
  • Could you give a bit more details as to what errors you have / what problems you're encountering and where, and what your actual question is? You said you're trying to compare two `xml` files with attributes and child elements and showed code but haven't asked an actual question or pointed to a problem, so this may be confusing to someone who can answer this. – Matt Nov 19 '19 at 16:20
  • 1
    I am trying to compare two xmls regardiess of 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 am expecting help on how to compare mentioned xmls i while gnoring tag orders – Kuleen Nov 19 '19 at 16:38

0 Answers0