0

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

  • Namespace declarations are not normal attributes, XMLUnit likely doesn't even see it (as an attribute, only its effect). What does the element you want to compare it to look like? I'm not sure I understand what you mean by ignoring it. – Stefan Bodewig Feb 03 '17 at 05:09
  • I use attribute filter and Node filter to ignore the attributes and nodes which i dont want to assert. So, I want to ignore this "xmlns:ns3="http://core.soayodlee.com">" while comparing the xml. Using the above code it is not working please help me – Anurag Patro Feb 03 '17 at 05:54
  • Namespace declarations are no normal attributes, you can't use an `AttributeFilter` on them. What does your other XML look like. Is `CoreExceptionFault` inside the same namespace (URI-wise) as in your snippet or is one XML without any namespaces at all? – Stefan Bodewig Feb 03 '17 at 08:33
  • isn't there a way I can ignore the namespace attributes, the first line is in this way ns3:CoreExceptionFault xmlns:ns3="http://core.soap.yodlee.com"> with which i need to compare – Anurag Patro Feb 03 '17 at 09:03
  • You probably don't see a `Difference` for the attribute at all, but for the elements as their namespace URI is different. You don't need to exclude the attribute but must provide a `DifferenceEvalutor` that swallows namespace differences. – Stefan Bodewig Feb 03 '17 at 10:36
  • Similar: https://stackoverflow.com/questions/26524286/xmlunit-ignore-xmlns and https://stackoverflow.com/questions/13358305/how-to-compare-two-xml-with-the-same-namespace-but-different-prefixes-using-java – Vadzim Jun 29 '17 at 16:41

1 Answers1

1

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 QNames.

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.

Stefan Bodewig
  • 3,260
  • 15
  • 22