Reputation: 499
I am trying to compare PUT Request XML
and GET Response XML
. The following code works so good but according to the requirements some elements are only readable and therefore appears in GET Response XML file. How can I eliminate such a following result?
Result:
[different] Expected presence of child node 'null' but was 'bs:key_id' - comparing at null to <bs:key_id...> at /container[1]/int_user_object[1]/attributes[1]/key_id[1]
What I tried:
import java.io.IOException;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.xml.sax.SAXException;
public class XMLComparator {
static Logger logger = LogManager.getLogger(XMLComparator.class);
public boolean compare(String xml1, String xml2) throws SAXException, IOException {
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreAttributeOrder(true);
DetailedDiff diff = new DetailedDiff(new Diff(xml1, xml2)); // wrap the Diff inside Detailed Diff.
List<?> allDiff = diff.getAllDifferences();
boolean result = diff.similar();
if (result == false) {
logger.info("There are " + allDiff.size() + " differences. XML difference(s): " + diff.toString());
} else {
logger.info("Sending XML and received XML are same: " + result);
}
return result;
}
}
I planned to add such a logic in my code after reading this document doc but I could not.
if(NODE_TYPE_ID == null){
difference.ignore();}
According to the Stefan help you can see the last version of my code
EDIT
public class XMLComparator {
static Logger logger = LogManager.getLogger(XMLComparator.class);
public boolean compare(String xml1, String xml2) throws SAXException, IOException {
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreAttributeOrder(true);
DetailedDiff diff = new DetailedDiff(new Diff(xml1, xml2));
diff.overrideDifferenceListener(new DifferenceListener() {
@Override
/* CHILD_NODE_NOT_FOUND_ID is used: A child node in one piece of XML could not be match against any other node of the other piece.
*
*/
public int differenceFound(Difference difference) {
return difference.getId() == DifferenceConstants.CHILD_NODE_NOT_FOUND_ID
? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL : RETURN_ACCEPT_DIFFERENCE;
}
@Override
public void skippedComparison(Node arg0, Node arg1) {
}
});
List<?> allDiff = diff.getAllDifferences();
boolean result = diff.identical();
if (result == false) {
logger.info("There are " + allDiff.size() + " differences. XML difference(s): " + diff.toString());
} else {
logger.info("Sending XML and received XML are same: " + result);
}
return result;
}
}
Upvotes: 0
Views: 1208
Reputation:
One approach is to implement a DifferenceListener
and suppress the differences that arise from missing nodes, something like
public int differenceFound(Difference difference) {
return difference.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID
? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL
: RETURN_ACCEPT_DIFFERENCE;
}
If you've got the choice of switching to XMLUnit 2.x, though, you could use a NodeFilter
to completely hide the elements you know are missing (rather than ignoring all missing elements).
Upvotes: 1