# 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:

- A simple Purchase Order suggesting how an on-line business might send and receive data.
- The XML specification was written in XML. With a prepublication version of its Document Type Definition (DTD) file, it's included here as a rather sophisticated example of how DTDs are used.
- Courtesy of Fuji Xerox, an short XML file in Japanese, using Japanese tags. This is a weekly report with its DTD, which can be validated. (More such samples are available on-line, at http://www.fxis.co.jp/DMS/sgml/xml/charset/.)
- A reverse-engineered DTD for CDF, the Channel Distribution Format used by Microsoft's IE4 to describe how browsers can pull content from servers in "pseudo-push" systems. It includes an example. (Note that the CDF files distributed by Microsoft with IE4 are neither well formed nor valid XML, unlike these samples.)
- From a large collection of XML sample documents at the sunsite.unc.edu FTP server, The Two Gentlemen of Verona and The Tragedy of Richard the Third. These include their DTD.
- A simple example showing the use of XML namespaces.

### 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

- How to build such documents;
- Deep and shallow cloning;

### 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

- Getting a SAX parser;
- Providing a document handler;
- Customizing its error handling;
- Use a validating parser.

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:

- Using a different default ElementNode class, which implements `swing.tree.TreeNode` and so can be used directly as a `swing.JTree` model.
- Some tags (PLAY, ACT, and SCENE, as specified by a widely available DTD for Shakespearean plays) have custom implementations to provide behaviours that are specific to objects in such documents.

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:

- Richard III
- Two Gentlemen of Verona

### 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.