Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Comparing XML Documents with Oracle's XDK 10g for Java : Page 2

Using Oracle's XDK 10g, you can reduce the process of comparing XML documents to a simple set of library calls that let you determine if the documents are the same, see what the differences are, or use the compare information to generate other documents.




Application Security Testing: An Integral Part of DevOps

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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date