java - Ignoring a particular attribute of a specific Node while comparing xml files using XMLUnit 2.X -
i have 2 xml files:
<------------------------file1---------------------------------> <note id="ignorethisattribute_1"> <to>experts</to> <from>matrix</from> <heading id="dontignorethisattribute_1">reminder</heading> <body>help me problem</body> </note>
<------------------------file2---------------------------------> <note id="ignorethisattribute_2"> <to>experts</to> <from>matrix</from> <heading id="dontignorethisattribute_2">reminder</heading> <body>help me problem</body> </note>
i have ignore attribute:id
of node:note
while comparing these 2 files.
i using diffbuilder
:
diff documentdiff = diffbuilder.compare(srcfile).withtest(destfile).build()
most online solutions suggested implementing differenceevaluator
:
tried well, ignores nodes attribute id, whereas want ignore attribute specific node:
public class ignoreattributedifferenceevaluator implements differenceevaluator { private string attributename; public ignoreattributedifferenceevaluator(string attributename) { this.attributename = attributename; } @override public comparisonresult evaluate(comparison comparison, comparisonresult outcome) { if (outcome == comparisonresult.equal) return outcome; final node controlnode = comparison.getcontroldetails().gettarget(); system.out.println(controlnode.getnodename()); if (controlnode instanceof attr) { attr attr = (attr) controlnode; if (attr.getname().equals(attributename)) { return comparisonresult.equal; } } return outcome; } }
calling method in test class:
final diff documentdiff = diffbuilder.compare(src).withtest(dest) .withdifferenceevaluator(new ignoreattributedifferenceevaluator("id")) .build();
can suggest me way can achieve using xmlunit 2.x new xmlunit please accordingly.
thanks in advance.
you use differenceevaluator
if wanted to. you'd have testing name of attr
's "owner element"'s name in addition name of attribute itself.
but xmlunit 2.x offers different solution this: attributefilter
. code won't different differenceevaluator
you've got, won't mixing concerns.
class ignorenoteid implements predicate<attr> { public boolean test(attr attr) { return !("note".equals(attr.getownerelement().getnodename()) && "id".equals(attr.getnodename())); } }
or shorter lambda in withattributefilter
when using java8.
Comments
Post a Comment