XML Examples in Java
CONTENTS
Introduction
Parsing XML
Parsing with SAX
Parsing with DOM
Generating XML
Generating XML with Print Statements
Generating XML with DOM
Generating XML with SAX
Transforming XML
Transforming XML with SAX Filters
Transforming XML with XSLT

Introduction

These examples show various techniques for processing XML with JAXB.
Note that you will need Java 1.4 to compile these examples.
Path names are currently hard-coded in the example files, so you'll need to modify these before compiling. Here's a simple example file that you can use for testing:
An XML file - in.xml: View Source Download

Parsing XML

The first set of examples show how to parse existing XML files.
There are two main approaches: SAX and DOM.

Parsing with SAX

The first approach, which uses SAX (Simple API for XML), essentially converts the XML document into a stream of events. You need to define a 'handler' that receives those events; it must implement the ContentHandler interface. In the following example, a basic handler that implements ContentHandler directly is used.
ParseUsingSAX.java: View Source Download
SAX also provides a default handler that implements ContentHandler and provides do-nothing methods for all of the necessary methods. We can simplify the example above by deriving our content handler from the default handler, and just implementing the methods that we need to override.
ParseUsingSAXDefaultHandler.java: View Source Download

Parsing with DOM

The second approach uses the DOM (Document Object Model), and builds a tree representation of the document in memory, which can then be traversed and manipulated.
ParseUsingDOM.java: View Source Download
In general, DOM uses far more memory than SAX, while programs using DOM are simpler than those that use SAX.

Generating XML

The second set of examples show how to generate XML files from scratch.

Generating XML with Print Statements

The naive approach is to simply use print statements. This approach works in for short documents, but there are several potential weaknesses. The biggest problem is to remember to escape the special characters that cannot appear in XML text, such as <. Another problem is simply a software engineering problem: different programmers will use different techniques for organizing the generation code, leading to endless confusion. It is much better to use a standardized approach.

Generating XML with DOM

One common technique that can be used is to build the document as a DOM tree in memory. This approach can be costly in terms of memory consumption, however, if the document is large. The following example shows this approach:
GenerateUsingDOM.java: View Source Download

Generating XML with SAX

Another approach is based on the idea of simulating a SAX parser. The idea is to generate a series of SAX events by calling methods in the ContentHandler interface. The resulting event stream can be converted directly into an XML document. The simplest way to perform the conversion is to use an identity XSLT transformation, and that's what we use in the following examples.
In the first example, we create a default implementation of XMLReader, and then a subclass which actually generates the SAX events. The event stream is converted to a file by means of an identity XSLT transformation.
GenerateUsingXMLReader.java: View Source Download
It's awkward to have to implement all the methods of XMLReader, though. Another approach is to use XMLFilter instead, since SAX conveniently provides a default implementation of this interface, namely XMLFilterImpl. The following example is considerably simpler:
GenerateUsingXMLFilter.java: View Source Download
Perhaps the simplest variation of this approach uses a TransformerHandler. Unfortunately, it's not so easy to example what is actually going on in this example!
GenerateUsingTransformerHandler.java: View Source Download

Transforming XML

Many large systems are based on the notion of streams of XML make their way through various components, and undergoing transformations in the process. There are two basic mechanisms for transforming a stream of XML: XML filters and XSLT transformations.

Transforming XML with SAX Filters

XML filters are based on the SAX model, and are typically hooked up to a SAX parser, so that the events generated by the parser pass through the filter on the way to the destination. Multiple filters can actually be chained together to apply various bits of processing as the stream passes through the pipeline.
The first example shows a single filter:
FilterWithCustomContentHandler.java: View Source Download
The second example shows a chain of filters:
ChainedContentHandlers.java: View Source Download

Transforming XML with XSLT

A different, and more general approach is to use XSLT transformations. In this approach, the required transformation is defined by means of an XSL file.
XSLT is in fact a functional programming language. Here's an XSLT file that can be applied to the example XML file accessible at the top of this page.
An XSLT file - trans.xsl: View Source Download
And here's an example that shows how to apply the transformation:
TransformUsingXSLT.java: View Source Download