1

I want to compare two XML in one of the Junit test. I am using XMLUnit for comparison of xml.Can you please tell me if there is any easy way to ignore comparison of correlation-id in the xmls.

XML1:

<?xml version="1.0" encoding="UTF-8"?>
<response>
<bih-metadata>
<result>Error</result>
<correlation-id>ID:925977d0-83cd-11e6-b94d-c135e6c73218</correlation-id>
<error-message>SAXParseException: The entity name must immediately follow the '&amp;' in the entity reference.</error-message>
</bih-metadata>
</response>

XML2:

<?xml version="1.0" encoding="UTF-8"?>
<response>
<bih-metadata>
<result>Error</result>
<correlation-id>ID:134345d0-83cd-11e6-b94d-c135e6c73218</correlation-id>
<error-message>SAXParseException: The entity name must immediately follow the '&amp;' in the entity reference.</error-message>
</bih-metadata>
</response>
ekad
  • 14,436
  • 26
  • 44
  • 46
Ravi
  • 1,247
  • 4
  • 15
  • 35

1 Answers1

0

This is what NodeFilter has been added for in XMLUnit 2.x:

import org.w3c.dom.Element;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.util.Nodes;
import org.xmlunit.diff.*;

public class Test {

    public static void main(String[] args) {
        Diff d = DiffBuilder.compare("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                                     "<response>\n" +
                                     "<bih-metadata>\n" +
                                     "<result>Error</result>\n" +
                                     "<correlation-id>ID:925977d0-83cd-11e6-b94d-c135e6c73218</correlation-id>\n" +
                                     "<error-message>SAXParseException: The entity name must immediately follow the '&amp;' in the entity reference.</error-message>\n" +
                                     "</bih-metadata>\n" +
                                     "</response>")
            .withTest("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                      "<response>\n" +
                      "<bih-metadata>\n" +
                      "<result>Error</result>\n" +
                      "<correlation-id>ID:134345d0-83cd-11e6-b94d-c135e6c73218</correlation-id>\n" +
                      "<error-message>SAXParseException: The entity name must immediately follow the '&amp;' in the entity reference.</error-message>\n" +
                      "</bih-metadata>\n" +
                      "</response>")
            .withNodeFilter(n -> !(n instanceof Element && "correlation-id".equals(Nodes.getQName(n).getLocalPart())))
            .build();
        System.err.println("Different? " + d.hasDifferences());
    }
}
Stefan Bodewig
  • 3,260
  • 15
  • 22