1

I am having 2 XML documents as follows

XML1:

<root>
<element>
<attributes>
<a>1</a>
<b>y</b>
<c>n</c>
<attribute>
<attributes>
<a>2</a>
<b>j</b>
<c>m</c>
<attribute>
<attributes>
<a>3</a>
<b>y</b>
<c>n</c>
<attribute>
<attributes>
<a>4</a>
<b>y</b>
<c>n</c>
<attribute>
<element>
<element2>
</element2>
</root>

**XMl2**:
<root>
<element>
<attributes>
<a>1</a>
<b>y</b>
<c>n</c>
<attribute>
<attributes>
<a>3</a>
<b>y</b>
<c>n</c>
<attribute>
<attributes>
<a>4</a>
<b>y</b>
<c>n</c>
<attribute>
<element>
<element2>
</element2>
</root>

I want to compare <attribute> child elements only when first child element <a> text is equal in both the XML

For eg., I want to compare <attribute> where <a> = 3 from XML1 with <attribute> where <a> = 3 from XML2.

Currently I am using below code.

Diff myDiff=null;

    DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();

    try {
        myDiff = new Diff(expected,actual);
        myDiff.overrideDifferenceListener(myDifferenceListener);
    } catch (SAXException | IOException e) {
        e.printStackTrace();
    }

   DetailedDiff myDiffd = new DetailedDiff(myDiff);
    List<Difference> allDifferences = myDiffd.getAllDifferences();

This is giving me wrong comparison. If <attribute> where <a> = 3 is missing from XML then this node and next all node shows as mismatch. But I want only this node as mismatch.

Any help is appreciated.

potame
  • 7,597
  • 4
  • 26
  • 33
  • `where = 3` is mentioned in the question couple of times. What does it mean? – Rao Sep 14 '16 at 03:17
  • Sorry, It was a mistake while adding code in post. I edited my original post. Thanks for pointing me out. – Goutham Shivakumar Nandipati Sep 21 '16 at 08:38
  • Hi Group, There are no one in group to address my query? – Goutham Shivakumar Nandipati Oct 03 '16 at 08:19
  • That looks like XMLUnit 1.x to me. Assuming you want to keep working with that, you'll need to implement `ElementQualifier` in order to only consider elements as candidates for a match when their values of `a` are equal. – jsheeran Oct 04 '16 at 14:08
  • in particular you want an `ElementQualifier` that matches on name (i.e. `ElementNameQualifier` unless your element is named `attributes` in which case you only want to match the nodes if the nested text of their `a` children match. This is doable in XMLUnit 1.x with custom code but will be a lot easier with XMLUnit 2.x - basically you are in the same situation as described in https://github.com/xmlunit/user-guide/wiki/SelectingNodes with `tr` being called `attributes` and `th` becoming `a` in you case. – Stefan Bodewig Nov 29 '16 at 12:02

0 Answers0