Using W3C DOM Documents

1. Introduction

W3C DOM object trees can be used to represent an XML document's structure. It may be convenient to:

  • pass DOM representations of an XML document as inputs to DeltaXML's comparison pipeline, and
  • get a DOM document as the result of a comparison.

This sample illustrates how DOM documents can be both passed as input and produced as output from a DeltaXML comparison pipeline.

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 DOM documents into Saxon XdmNode trees (using the Saxon processor)
  4. Perform the comparison;
  5. Convert the result into a DOM document.

2.1. Preparing a Saxon processor

We construct a new saxon processor as follows:

Processor proc= new Processor(false);

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);

Note, if the supplied Saxon processor is a Saxon HE (Home Edition) processor then there may be compatibility issues if the pipeline uses any filters which in turn use Saxon PE or EE features.

2.3. Converting a DOM document into a Saxon XdmNode tree

We make use of a Saxon processor to convert a W3C DOM document (doc) from location (loc) into Sxaon XdmNode tree as follows:

DOMSource source= new DOMSource(doc, loc);
XdmNode xdmNode= proc.newDocumentBuilder().build(source);

2.4. Preparing a DOM destination

We create a Saxon DOM destination as follows:

2.5. Comparing the XdmNode trees

We perform the DeltaXML comparison as follows.

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

2.6. Convert the resulting XdmNode into a DOM document

We create and populate a DOM document as follows:

Document doc= docBuilder.newDocument();
DOMDestination dest= new DOMDestination(doc);
proc.writeXdmValue(result, dest);

3. Running the sample

The sample code essentially loads two input files (input1.xml and input2.xml) into DOM documents, compares them, produces an output DOM 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 or the ant run commands.

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.

javac -cp ../../deltaxml.jar:../../saxon9pe.jar UsingDOM.java
java -cp .:../../deltaxml.jar:../../saxon9pe.jar UsingDOM 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 commentry in the UsingDOM.java source file.