XML Examples

This release includes a number of examples showing XML and how it can be used:
  1. Sample XML Files
  2. Simple File Parsing
  3. Building XML Documents with DOM
  4. Using SAX Directly
  5. XML Namespace Support
  6. Swing JTree Display
  7. Text Transcoding

Of course, this only scratches the surface of the kinds of things you can do with XML.


Sample XML Files

A handful of sample XML files have been provided in the "samples" directory:

Simple File Parsing

One of the first things most programmers want to know is how to read and generate a DOM document object from it. Use the simple example to learn how to do this. The important lines are:
	// Create an instance of the DocumentBuilderFactory
	DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.
								newInstance();
	//Get the DocumentBuilder from the factory that we just got above.
	DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

	// turn it into an in-memory object
	doc = docBuilder.parse(new File (argv [0]));


Building XML Documents with DOM

This library includes basic support for building XML documents using the W3C Document Object Model (DOM) interfaces. A simple file, main.java, demonstrates

Using SAX Directly

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.


XML Namespace Support

This library includes basic support for accessing XML namespace information. XML namespaces support a simple lexical scoping mechanism to help assign a "namespace" to elements and attributes. Such namespaces are URIs, such as http://www.example.com/. A simple file, main.java, demonstrates how to access namespace information.

Note that accessing XML namespaces is not part of DOM Level 1; this is an (essential) extension.


for Swing JTree Display

This example uses two kinds of element customizations:

There is a common driver, which can be given different customization and program data. One command shows "Richard III" as a tree of XML data. Another shows "Two Gentlemen of Verona" using custom element classes to control the structure that's displayed. (See the Makefile; you may need to modify this to point to the version of SWING which you're installed, if you're not using a JDK 1.2 release.)

    $ make doit1
    $ make doit2

Alternatively, these are accessible as applets from viewers (such as the JDK 1.2 "appletviewer") which support JFC 1.1:


Text Transcoding

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.