Reputation: 11
this is my code
Scanner s = new Scanner(new File("ignore.txt"));
final ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
}
s.close();
int lengthList = list.size();
final Set<String> values = new HashSet<String>(list);
Diff myDiff1 = DiffBuilder.compare(writer1.toString()).withTest(writer.toString())
.checkForSimilar()
.checkForIdentical()
.ignoreWhitespace()
.normalizeWhitespace()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
.withNodeFilter(new Predicate<Node>() {
public boolean test(Node n) {
String temp = Nodes.getQName(n).getLocalPart();
//System.out.println(temp);
return !(n instanceof Element &&
values.contains(temp));
}
})
.withAttributeFilter(new Predicate<Attr>(){
public boolean test(Attr n){
javax.xml.namespace.QName attrName = Nodes.getQName(n);
//System.out.println(attrName.toString());
//QName Temp = new QName();
//System.out.println(Temp.toString()+n.toString());
Boolean temp1 = !values.contains(attrName.toString());
//Boolean temp1 =!attrName.toString().equals("answerType") ;
//System.out.println(temp1);
//return !attrName.equals(new QName("balType",null, null, "curCode"));
return temp1;
}
})
.build();
my xml file is
<ns3:CoreExceptionFault xmlns:ns3="http://core.soayodlee.com">
<faultText>com.yodlee.util.system.ycache.CobrandNotSupportedException: The CobrandCache for the cobrand 10004 is not initialized.</faultText>
</ns3:CoreExceptionFault>
I want to ignore the xmlns:ns3 attribute. The above file Ignore.txt contains all the node and attributes which i need to ignore. But when I am adding xmlns:ns3 it is not ignoring the attribute. I am using XMLunit 2.2.1
Upvotes: 0
Views: 2397
Reputation:
The xmlns:
attributes are not "normal" attributes, you can't ignore them and XMLUnit won't report differences about them either. They are meta-attributes that apply to the element (and its children) and are usually hidden from XMLUnit by the XML parser.
In your case you seem to be comparing XML documents with elements using different namespace URIs. Be warned that two such documents will be different for any namespace aware XML parser.
If you really want to make them pass as similar, you'll have to use a DifferenceEvaluator
that ignores the namespace part of the element's QName
s.
Something like
.withDifferenceEvaluator(DifferenceEvaluators.chain(
DifferenceEvaluators.Default, new MyEvaluator())
with something like
class MyEvaluator implements DifferenceEvaluator {
@Override
public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) {
if (comparison.getType() == ComparisonType.NAMESPACE_URI) {
return ComparisonResult.SIMILAR;
}
return outcome;
}
}
should work.
BTW, you should only specify one of checkForSimilar()
and checkForIdentical()
, they contradict each other and only the last one wins.
Upvotes: 1