Introduction
In an earlier tutorial ("XML programming in Java technology, Part 1"), I showed you the basics of XML parsing in the Java language. I covered the major APIs (DOM, SAX, and JDOM), and went through a number of examples that demonstrated the basic tasks common to most XML applications. This tutorial will look at more difficult things that weren't covered before, such as:
- Getting and setting parser features
- Working with namespaces
- Validating XML documents
As in the introductory tutorial, the APIs I'll cover are:
- The Document Object Model (DOM), Levels 1, 2, and 3
- The Simple API for XML (SAX), Version 2.0
- JDOM, a simple Java API created by Jason Hunter and Brett McLaughlin
- The Java API for XML Processing (JAXP)
I'll also cover several approaches to validation, including W3C XML Schema, RELAX NG, and Schematron.
Most of the examples here will work with the Shakespearean sonnet that appeared in the last tutorial. The structure of this sonnet is:
<sonnet>
<author>
<lastName>
<firstName>
<nationality>
<yearOfBirth>
<yearOfDeath>
</author>
<lines>
[14 <line> elements]
</lines>
</sonnet>
|
In the various sample programs, some versions of this document will have namespaces, and some will use DTDs, W3C XML Schemas, or other schema languages for validation. For the complete examples, see the following files:
- sonnet.xml
- sonnet.dtd (download to view in a text editor)
- sonnetNamespaces.xml
- sonnet.xsd
- sonnetSchema.xml
- sonnet.rng
- sonnetRules.xsl
- sonnetSchematron.xml
As an alternative, download x-java2_code_files.zip to view these files in a text editor.
You'll need to set up a few things on your machine before you can run the examples. (I'm assuming that you know how to compile and run a Java program, and that you know how to set your CLASSPATH variable.)
- First, visit the home page of the Xerces XML parser at the Apache XML Project (http://xml.apache.org/xerces2-j/). You can also go directly to the download page (http://xml.apache.org/xerces2-j/download.cgi).
- Unzip the file that you downloaded from Apache. This creates a directory named
xerces-2_5_0or something similar, depending on the release level of the parser. The JAR files you need (xercesImpl.jarandxml-apis.jar) should be in the Xerces root directory. - Visit the JDOM project's Web site and download the latest version of JDOM (http://jdom.org/).
- Unzip the file you unloaded from JDOM. This creates a directory named
jdom-b9or something similar. The JAR file you need (jdom.jar) should be in thebuilddirectory. - Finally, download the zip file of examples for this tutorial, x-java2_code_files.zip , and unzip the file.
- Add the current directory (
.),xercesImpl.jar,xml-apis.jar, andjdom.jarto yourCLASSPATH.


