WEBINAR:
On-Demand
Building the Right Environment to Support AI, Machine Learning and Deep Learning
Comparing XML Documents with the XMLDiff Class
To compare two documents, you first, import the XMLDiff class package oracle.xml.differ into a Java application as shown below.
import oracle.xml.differ.*;
Next, create a DOMParser to parse the XML documents to be compared. The DOMParser class extends the oracle.xml.parser.v2.XMLParser class.
DOMParser parser=new DOMParser();
You have to load and parse the documents, using one of the
parse() methods in the XMLParser class. You can load and parse an XML document from an InputSource, InputStream, Reader, String, or URL. I've used an InputStream in this article. Create an InputStream object from the XML document
catalog.xml and parse the document with the
parse(InputStream) method.
InputStream catalog1=new FileInputStream(new
File("C:/XMLDiff/catalog.xml"));
parser.parse(catalog1);
Obtain the XMLDocument object corresponding to the XML document parsed.
XMLDocument xmlDocument1=parser.getDocument();
Similarly, create an InputStream for XML document
catalog2.xml and parse the XML document. Next, obtain an XMLDocument object for the XML document,
catalog2.xml as shown below.
InputStream catalog2=new FileInputStream(new
File("C:/XMLDiff/catalog2.xml"));
parser.parse(catalog2);
XMLDocument xmlDocument2=parser.getDocument();
The XMLDiff class is an interface for comparing two XML documents. You'll need to create a XMLDiff class. The
downloadable sample Java application compares the two sample XML documents, and extends the XMLDiff class as the XMLCompare class. Here's how you create an XMLCompare class object.
XMLCompare xmlDiff=new XMLCompare();
Specify the XMLDocuments to be compared, either as oracle.xml.parser.v2.XMLDocument class objects, or as java.io.File objects. You can set them as XMLDocument objects with the
setDocuments(XMLDocument, XMLDocument) method or (using two calls)
setInput1(XMLDocument) and
setInput2(XMLDocument) methods. Alternatively, you can set them as File objects using the
setFiles(File, File) method, or the two
setInput1(File) and
setInput2(File) methods. The sample code uses the
setDocuments(XMLDocument, XMLDocument) method as shown below.
xmlDiff.setDocuments(xmlDocument1, xmlDocument2);
You compare the two example XML documents with the
diff() method, which returns a
boolean.
boolean diff=xmlDiff.diff();
If the value of the
diff variable is
false, the two XML documents are the same, while if
diff is
true, the documents are different. Using the example documents shown in
Listing 1 and
Listing 2, you'll get a value of
true for the
diff variable, which indicates that the documents are different.
You can also compare nodes using the
equals(Node, Node) method, which also returns a
boolean.
Now that you know the documents are different, you might be interested in listing the actual differences. You can generate a listing of the differences using the
printDiffTree(int, BufferedWriter) method. The
int parameter specifies which XML document to use as the base document when evaluating the differences. In other words, if the
int parameter value is
1, the
printDiffTree() method outputs the additions/deletions/modifications in XML document 1 as compared to XML document 2, while if
int is
2, the method outputs the differences for document 2 as compared to document 1. The
BufferedWriter parameter specifies the output file. To obtain the results, specify the
int parameter value as
1 and create a BufferedWriter to output the differences between the XML documents, as shown below.
BufferedWriter bufferedWriter=new BufferedWriter(
new FileWriter(new File("c:/XMLDiff/diff.txt")));
xmlDiff.printDiffTree(1, bufferedWriter);
bufferedWriter.flush();
A BufferedWriter outputs the set of features that are different between the two XML documents.
Listing 3 shows the sample output containing the differences between the two example documents.
The output is fairly straightforward. The
MODIFIED keyword indicates XML document elements that are present in both documents, but that differ (are modified) between the two. Added elements are indicated by the keyword
ADDED, and deleted elements by
DELETED. Note that the elements that are marked
DELETED would be marked
ADDED, and elements marked
ADDED would be marked
DELETED if the int value in
printDiffTree() method is modified from 1 to 2.