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 and Redisplay
  3. Building XML Documents with DOM
  4. Using SAX Directly
  5. XML Namespace Support
  6. XML Validation Service
  7. Swing JTree Display
  8. XML Messaging: HTTP(S) and POST
  9. 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 and Redisplay

One of the first things most programmers want to know is how to read and write XML Documents. Use the simple example to learn how to do this. The important lines are:
	// turn the filename into an input source
	input = Resolver.createInputSource (new File (argv [0]));

	// turn it into an in-memory object
	// ... the "false" flag says not to validate
	doc = XmlDocument.createXmlDocument (input, false);

	// normalize text representation
	doc.getDocumentElement ().normalize ();

	// prettyprint
	doc.write (System.out);

Note that there are more flexible ways to create such documents and to customize their in-memory representations. Prettyprinting can also be disabled, as can the whitespace-stripping parts of normalizing text representations.


Building XML Documents with DOM

This library includes basic support for building XML documents using the W3C Document Object Model (DOM) interfaces. The classes implementing that support also support writing the documents as text. A simple file, main.java, demonstrates

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


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 org.xml.sax.parser system property to com.sun.xml.parser.ValidatingParser.


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.


XML Validation Service

In much the same way that the W3C offers an HTML validation service, it's straightforward to use this library to construct an XML validation service.

Note that this service will not be available except in web servers such as the Java Web Server 1.1, which allow Java code to be embedded in HTML text for server side execution. Even then it requires the XML library to have been installed in the web server, and installation of this sample page someplace other than a user's home directory. (Java Server Pages, both in their original "page compiled" syntax and the newer JSP syntax, are normally sandboxed so that they can't access network or filesystem resources.)


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:

Later updates may improve the way this text renders; the current code was optimized for speed of writing, rather than execution time or memory utilization.


XML Messaging: HTTP(S) and POST

A simple client side method can exchange XML documents with servers using an HTTP (or HTTPS) POST operation, returning the resulting XML document to the caller. On the server side, a simple servlet can receive such XML request documents and send XML documents in response. Such invocations can transparently pass between firewalls, and can be used as the basis for general purpose inter-application messaging. See:

Another interesting approach involves sending and receiving XML messages (perhaps XML/EDI ones) using the Java Messaging Service (JMS) API. This release does not include code specifically supporting such approaches, though the code in this example for sending and receiving XML documents should be easily adapted to such tasks.


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.