Using XOM

1. 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 DeltaXML'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 DeltaXML comparison pipeline.

Note, XOM libraries are not shipped with the DeltaXML product, therefore the user needs to supply a XOM library, which can be obtained from http://www.xom.nu/.

2. Outline of the Approach

Essentially the approach is to:

  1. Prepare a Saxon processor
  2. Prepare a DeltaXML comparator pipeline (which uses the Saxon processor)
  3. Convert the input XOM documents into Saxon XdmNode trees (using the Saxon processor);
  4. Perform the comparison;
  5. Convert the result into a XOM document.

2.1. 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();

2.2. Preparing a DeltaXML comparator

We create a DeltaXML pipelined comparator that uses the existing Saxon processor as follows:

PipelinedComparatorS9 pc= new PipelinedComparatorS9(proc);

2.3. 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 Sxaon XdmNode tree as follows:

DocumentWrapper wrap= new DocumentWrapper(doc, loc, config);
XdmNode xdmNode1= proc.newDocumentBuilder().build(wrap);

2.4. Comparing the XdmNode trees

We perform the DeltaXML comparison as follows.

XdmNode result= pc.compare(xdmNode1, xdmNode2);

2.5. 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();

3. Running the sample

The sample code essentially loads two input files (input1.xml and input2.xml) into XOM documents, compares them, produces an output XOM documents, which is then saved to a file (output.xml).

The sample is designed to be built and run via the Ant build technology. The provided build.xml script has two main targets

  • run - the default target which compiles and runs the sample code.
  • clean - returns the sample to its original state.

It can be run by issuing either the ant -Dxom=jar=<path-to-xom.jar>or the ant -Dxom=jar=<path-to-xom.jar>run commands, where<path-to-xom.jar>is the path to the user supplied XOM library.

Alternatively, the sample can be manually compiled and run using the following Java commands, asuming that both the Java compiler and runtime platforms are avaialble on the command-line, and that <path-to-xom.jar> is the path to the user supplied XOM library.

javac -cp ../../deltaxml.jar:../../saxon9pe.jar:<path-to-xom.jar> UsingXOM.java XOMDestination.java
java -cp .:../../deltaxml.jar:../../saxon9pe.jar:<path-to-xom.jar> UsingXOM input1.xml input2.xml output.xml

Note that you need to ensure that you use the correct directory and class path separators for your operating system.

4. Further Details

See the commentary in the UsingXOM.java and XOMDestination.java source file.