# XML Examples

This release includes examples showing XML and how it can be used:

1. Sample XML Files
2. Printing a DOM Tree
3. SAX Program to Count Tags

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

### Sample XML Files

A handful of sample XML files have been provided in the "samples" subdirectory. Note that the links may not work depending on your browser environment. Please look in the "examples/samples" directory if the links do not display in your browser.

- A simple Purchase Order (`book-order.xml`) suggesting how an on-line business might send and receive data.
- The original XML specification (`REC-xml-19980210.xml`) was written in XML. With a prepublication version of its Document Type Definition (DTD) file (`spec.dtd`), it's included here as a rather sophisticated example of how DTDs are used.
- Courtesy of Fuji Xerox, a short XML file in Japanese, using Japanese tags. This is a weekly report (`weekly-euc-jp.xml`) with its DTD (`weekly-euc-jp.dtd`), which can be validated.
- From a large collection of XML sample documents at the sunsite.unc.edu FTP server, The Two Gentlemen of Verona (`two_gent.xml`) and The Tragedy of Richard the Third (`rich_iii.xml`). These include their DTD (`play.dtd`).
- A simple example showing the use of XML namespaces (`namespace.xml`).

### Printing a DOM Tree

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 and setNamespaceAware
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);

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

### SAX Program to Count Tags

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.