package de.tutorials;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.DifferenceConstants;
import org.custommonkey.xmlunit.DifferenceListener;
import org.custommonkey.xmlunit.XMLUnit;
import org.w3c.dom.Node;
public class XmlDiffExample {
public static void main(String[] args) throws Exception{
String xmlA = "<root><elements><e/><e/><e/><e/></elements></root>";
String xmlB = "<root><elements><e/><e/><bubu/><e/><e/></elements></root>";
final DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(xmlA, xmlB));
diff.overrideDifferenceListener(new DifferenceListener() {
@Override
public void skippedComparison(Node control, Node test) {
}
@Override
public int differenceFound(Difference difference) {
if(difference.getId() == DifferenceConstants.CHILD_NODELIST_LENGTH_ID
&& difference.getControlNodeDetail().getNode().getNodeName().equals("elements") ){
return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
}else if(difference.isRecoverable()){
return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
}else if(difference.getTestNodeDetail().getNode().getNodeName().equals("bubu")){
return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
}
return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
}
});
System.out.println(diff.identical());
System.out.println(diff.similar());
}
}