The most recent example programs include a cross-platform ant build file that
can be used to build and run the example. Ant is a build tool
similar to make
on Unix and nmake
on
WindowsNT that is also an XML application. To use ant, download it
from the website and read the install docs. Alternatively, you can
also view the ant build.xml
file to see what needs to be
done to manually compile and run an example program on your
platform.
Older example programs that are marked "needs updating" are also
included and generally do not have ant
build files.
They are included here in case you may want to modify them on your
own.
One of the first things many programmers want to know is how to read an XML file and generate a DOM Document object from it. Use the DOMEcho example to learn how to do this in three steps. The important lines are:
// Step 1: create a DocumentBuilderFactory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // Step 2: create a DocumentBuilder DocumentBuilder db = dbf.newDocumentBuilder(); // Step 3: parse the input file to get a Document object Document doc = db.parse(new File(filename));
The program also gives an example of using an error handler and of setting optional configuration options, such as validation. Finally, this program allows you to understand the DOM itself, because it prints out the structure and contents of a DOM tree.
The SAXTagCount program counts the number of "tags" AKA elements in an XML document. This example also shows one way to turn on validation and how to use a SAX ErrorHandler.
There are several ways to parse a document using SAX and JAXP. We show one approach in this example. The first step is to bootstrap a parser. There are two basic ways: one is to use only the SAX API, the other is to use the JAXP utility classes in the javax.xml.parsers package. We use the second approach here because it allows the application to use a platform default implementation without having to specify a system property. After bootstrapping, there are several ways to begin a parse. In this example, we use the SAX API.
Needs updating: This example uses deprecated SAX 1.0 classes.
Sometimes you need to use SAX directly. In this library, SAX is normally used in conjunction with DOM. However, you may wish to use SAX directly, perhaps to build a data structure fully customized to your application. A simple file, main.java, demonstrates Try switching the default parser when you use this example,
by setting the javax.xml.parsers.validation
system property
to true
.
Text Transcoding
Needs updating
One of the features of XML is built in support for a wide variety
of document character sets. All XML processors are required to
support UTF-8 and UTF-16, and most support many more. As a rule,
XML processors in Java support the entire range of character sets
found in the JDK (well over 100 different ones!) both for input
and for output.
This simple example shows how an XML document can be read and converted to use a different document encoding. (This is called transcoding. This can't be done with all text formats, since they weren't all specified to support labeling or autodetection of the document character set; XML does support this.
Read the source to this program
to see how the encoding detection support of this parser can be
used to support transcoding.