Browse DevX
Sign up for e-mail newsletters from DevX


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

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Generating an XSLT Stylesheet
You can generate an XSLT stylesheet from the element/attribute differences between the example XML documents. To create an XMLDocument object, use the generateXSLDoc() method. To create it as a file, use the generateXSLFile(java.lang.String filename) method.


The XSLT file generated is illustrated in Listing 4, although I've added all the white space for readability.

You can use the XSLT document generated from the differences between the two example documents (see Listing 3) to update the first document to the second document. For example, to apply the diff.xslt file to the XML document catalog.xml, you can use the following command:

>oraxsl catalog.xml diff.xslt

That command generates the XML document catalog2.xml.

Alternatively, you can apply the XSLT stylesheet to some other document that may have additional differences from the second example document. For example, applying the stylesheet diff.xslt would update only the modified elements/attributes (including those added or removed) between catalog.xml and catalog2.xml.

Additionally, you can use the generated XSLT stylesheet to generate an XML document that consists solely of the modified attribute and element values between the two input XML documents. To do that, apply the XSLT to an XML document that does not specify values for any of the attributes and elements as shown below.

<?xml version="1.0" encoding="utf-8"?> <catalog> <journal title="" publisher="" edition=""> <article section=""> <title></title> <author></author> </article> </journal> <journal title="" publisher="" edition=""> <article section=""> <title></title> <author></author> </article> </journal> <journal></journal> </catalog>

To apply the diff.xslt to the preceding XML document with null values, use the command:

>oraxsl catalog-null.xml diff.xslt >catalog-diff.xml

The preceding command generates an XML document containing only modified attributes and elements as shown here:

<?xml version = '1.0' encoding = 'utf-8'?> <catalog> <journal edition="Jan-Feb 2005"> <article section=""> <title>Understanding Optimization</title> <author/> </article> </journal> <journal edition="March-April 2005"> <article section="XML"> <title>Aggregate Data with XQuery</title> <author>Nilesh Junnarkar</author> </article></journal> <journal/><journal/> </catalog>

Listing 5 shows the sample XMLCompare.java program used to compare the two example XML documents.

As you can see, the XDK 10g Production package contains everything you need to compare two XML documents in Java, list the differences between them, make one document match the other, or create a new document containing only the attributes and elements that differ.

Deepak Vohra is an O'Reilly technical reviewer, who reviewed the book WebLogic: The Definitive Guide. He's also a NuBean consultant and web developer, and is a Sun Certified Java 1.4 Programmer and Sun Certified Web Component Developer for J2EE.
Thanks for your registration, follow us on our social networks to keep up-to-date