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.