Versim XML Differencing


public class XMLDiff {
  public List diff(Node left, Node right);

  public void ignoreAttributes(boolean ignore);
  public void ignoreComments(boolean ignore);
  public void ignoreEmptyText(boolean ignore);
  public void ignoreProcessingInstructions(boolean ignore);
  public void ignoreText(boolean ignore);
  public void ignoreWhitespace(boolean ignore);

  public void ignoreNodes(Predicate predicate);

  public void setDetailedText(boolean on);
  public void orderNodes(Predicate predicate, Comparator comparator);
}
This is an XML differencer. During the difference calculation it iterates over both XML documents. They could be very large, so making copies to work on is going to be expensive both in terms of time and memory. The two documents in question might be part of some other processing, so we can't mutate them. Ideally, we want to make a single pass and get everything we need.

We do that custom iterators, and big set of predicates. The ignore* functions here set different predicates on the filter. Note that the library users can also provide their own predicates in addition to the settings provided. Those predicates can be (and are) arbitrarily complex, including making references to stuff entirely outside the document or even the toolkit. But it doesn't matter, because my filter will take care of it all.

The setDetailedText and orderNodes methods don't add predicates, they change the iterator used. Again they can be used together. The ordering iterator does some checking and caching to return nodes in a different sequence the how they occur in the document. The detailed text iterator steps over text nodes one (whitespace seperated) word at a time. The core differencing routine doesn't change. I was able to add both of these without changing the core at all.

By way of contrast, the DeltaXML xml diff requires you to modify the documents to achieve these same effects. If you want to ignore comments, then write a stylesheet to strip them. If you to do order-independent comparision, then annotate the document. For some things you need to do both.