Using XOM
Introduction
XOM trees can be used to represent an XML document's structure. It may be convenient to:
pass XOM representations of an XML document as inputs to XML Compare's comparison pipeline, and
get a XOM document as the result of a comparison.
This sample illustrates how XOM documents can be both passed as input and produced as output from a comparison pipeline. Resources are available on Bitbucket, see below for details.
Note, XOM libraries are not shipped with the XML Compare product, therefore the user needs to supply a XOM library, which can be obtained from http://www.xom.nu/.
Outline of the Approach
Essentially the approach is to:
Prepare a Saxon processor
Prepare a comparator pipeline (which uses the Saxon processor)
Convert the input XOM documents into Saxon XdmNode trees (using the Saxon processor);
Perform the comparison;
Convert the result into a XOM document.
Preparing a Saxon processor
We construct a new Saxon processor as follows, ensuring that we use a PE capable processor as this is required for Saxon's XOM support.
Processor proc= new Processor(true);
Configuration config= proc.getUnderlyingConfiguration();
Preparing a XML Compare comparator
We create a pipelined comparator that uses the existing Saxon processor as follows:
PipelinedComparatorS9 pc= new PipelinedComparatorS9(proc);
Converting a XOM document into a Saxon XdmNode tree
We make use of a Saxon processor to convert a XOM document (doc
) from location (loc
) into Saxon XdmNode tree as follows:
DocumentWrapper wrap= new DocumentWrapper(doc, loc, config);
XdmNode xdmNode1= proc.newDocumentBuilder().build(wrap);
Comparing the XdmNode trees
We perform the XML comparison as follows.
XdmNode result= pc.compare(xdmNode1, xdmNode2);
Convert the resulting XdmNode into a XOM document
We create a XOM document from the resulting Saxon XdmNode as follows:
XOMDestination dest= new XOMDestination();
proc.writeXdmValue(result, dest);
XOMWriter writer= (XOMWriter)dest.getReceiver(config);
Document output= writer.getDocument();
Running the sample
The sample resources and a description on how to run it can be found at: https://bitbucket.org/deltaxml/using-xom
The resources should be checked-out, cloned or downloaded and unzipped into the samples directory of the XML Compare release. They should be located such that they are two levels below the top level release directory, for example DeltaXML-XML-Compare-10_0_0_j/samples/usingXOM
.
Further Details
See the commentary in the UsingXOM.java and XOMDestination.java source file.