
s a J2EE developer you might sometimes be interested in comparing a modified XML document with the pre-modified version of that document. Or you might want to compare two XML documents that are both based on the same DTD or XML schema. This article is designed to give you the information you need to answer two questions:
-
Is one XML document the same as another?
- What are the differences between two XML documents?
In this article, I'll show you how to use
XDK 10g Production, which provides a mechanism to compare two XML documents and list the differences between them.
Oracle XML Developer's Kit (XDK) 10g is a set of APIs, tools and utilities to help you develop XML applications. XDK 10g is available for Java, C and C ++; however, this article discusses only the Java-compatible version. XDK 10g supports DOM 3.0 and XSLT 2.0 specifications. XDK 10g provides a schema validation API to validate XML documents against an XML schema. You can use the XML Class generator API in XDK 10g to bind XML schema to Java classes, and subsequently marshal and unmarshal XML documents. The XML SQL Utility aids in storing XML documents in a RDBMS database. A supplied XSQL servlet combines XML, SQL, and XSLT to generate an XML document from a SQL query.
Preliminary Setup
To get started, first download Oracle's
XDK 10g. You need to add the XMLDiff class package to your classpath. Add both
<XDK>/lib/xml.jar and
<XDK>/lib/xmlparserv2.jar to the classpath, where <XDK> is the directory in which you installed XDK 10g.
Using the XMLDiff Class
You use the XMLDiff class in the
oracle.xml.differ package to compare two XML documents. The class contains methods to compare two XML documents and enumerating the differences between them. In addition, you can choose to generate an XSLT stylesheet consisting of the differences between the two XML documents. You can then use the stylesheet to convert one of the compared XML documents to the other. This article compares an example XML document named
catalog.xml (see
Listing 1) with another XML document called
catalog2.xml (see
Listing 2).
Download the
sample code that accompanies this article, create a directory named
XMLDiff, and copy the two XML documents
catalog.xml and
catalog2.xml to that directory.
In comparing XML documents the empty (white) spaces in the XML document are also considered as nodes. You should remove white space in XML documents you want to compare if the white space is not required for comparison.