I have an XML file which looks like below.
Expected XML
<doc>
<tag>
<file>a.c</file>
<line>10</line>
<type>c</type>
<tag>
<tag>
<file>b.h</file>
<line>14</line>
<type>h</type>
<tag>
<tag>
<file>d.he</file>
<line>49</line>
<type>he</type>
<tag>
</doc>
Now XML for testing
<doc>
<tag>
<file>a1.c</file>
<line>10</line>
<type>c</type>
<tag>
<tag>
<file>b1.h</file>
<line>14</line>
<type>h</type>
<tag>
<tag>
<file>d1.he</file>
<line>49</line>
<type>he</type>
<tag>
</doc>
I want to compare this file with another XML file which has same structure.
I am using xmlUnit for comparison. While comparing I want to ignore XML tag <file>
Below is the comparison code which I wrote
public static Diff compareXML(String expXMLPath, String genXMLPath)
throws IOException, SAXException {
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
final List<String> ignorableXPathsRegex = new ArrayList<String>();// list of regular expressions that custom difference listener used during xml
//comparison
ignorableXPathsRegex
.add("\\/doc\\[1\\]\\/tag\\[1\\]\\/file\\[1\\]\\/text()");
Diff diff = null;
try(FileInputStream fileStream1 = new FileInputStream(expXMLPath)) {
try(FileInputStream fileStream2 = new FileInputStream(genXMLPath)) {
InputSource inputSource1 = new InputSource(fileStream1);
InputSource inputSource2 = new InputSource(fileStream2);
diff = new Diff(inputSource1, inputSource2);
RegDiffListener ignorableElementsListener = new RegDiffListener(
ignorableXPathsRegex);
diff.overrideDifferenceListener(ignorableElementsListener);
return diff;
}
}
}
This is not working if XML file has more than one <tag>...</tag>
block. I basically need a regex here which ignores all the <file>
tag which are under <doc><tag>
I want the comparison of expected and test XML to show that both are same by ignoring the value of file tag, so diff.similar()
should return true
Please suggest how to do it.