The JavaTM Web Services Tutorial
Home
TOC
PREV TOP NEXT

Echoing an XML File with the SAX Parser

In real life, you are going to have little need to echo an XML file with a SAX parser. Usually, you'll want to process the data in some way in order to do something useful with it. (If you want to echo it, it's easier to build a DOM tree and use that for output.) But echoing an XML structure is a great way to see the SAX parser in action, and it can be useful for debugging.

In this exercise, you'll echo SAX parser events to System.out. Consider it the "Hello World" version of an XML-processing program. It shows you how to use the SAX parser to get at the data, and then echoes it to show you what you've got.


Note: The code discussed in this section is in Echo01.java. The file it operates on is slideSample01.xml. (The browsable version is slideSample01-xml.html.)

Creating the Skeleton

Start by creating a file named Echo.java and enter the skeleton for the application:

public class Echo	
{	
   public static void main(String argv[])	
   {
 
       }
 
}
 

Since we're going to run it standalone, we need a main method. And we need command-line arguments so we can tell the app which file to echo.

Importing Classes

Next, add the import statements for the classes the app will use:

import java.io.*;	
import org.xml.sax.*;	
import org.xml.sax.helpers.DefaultHandler;	
import javax.xml.parsers.SAXParserFactory; 	
import javax.xml.parsers.ParserConfigurationException;	
import javax.xml.parsers.SAXParser;
 
public class Echo	
{	
   ...
 

The classes in java.io, of course, are needed to do output. The org.xml.sax package defines all the interfaces we use for the SAX parser. The SAXParserFactory class creates the instance we use. It throws a ParserConfigurationException if it is unable to produce a parser that matches the specified configuration of options. (You'll see more about the configuration options later.) The SAXParser is what the factory returns for parsing, and the DefaultHandler defines the class that will handle the SAX events that the parser generates.

Setting up for I/O

The first order of business is to process the command line argument, get the name of the file to echo, and set up the output stream. Add the text highlighted below to take care of those tasks and do a bit of additional housekeeping:

public static void main(String argv[])	
	
{	
   if (argv.length != 1) {	
      System.err.println("Usage: cmd filename");	
      System.exit(1);	
   }	
   try {	
      // Set up output stream	
      out = new OutputStreamWriter(System.out, "UTF8");
 
        } catch (Throwable t) {	
      t.printStackTrace();	
   }	
   System.exit(0);	
}	
	
static private Writer out;
 

When we create the output stream writer, we are selecting the UTF-8 character encoding. We could also have chosen US-ASCII, or UTF-16, which the Java platform also supports. For more information on these character sets, see Java Encoding Schemes (page 527).

Implementing the ContentHandler Interface

The most important interface for our current purposes is the ContentHandler interface. That interface requires a number of methods that the SAX parser invokes in response to different parsing events. The major event handling methods are: startDocument, endDocument, startElement, endElement, and characters.

The easiest way to implement that interface is to extend the DefaultHandler class, defined in the org.xml.sax.helpers package. That class provides do-nothing methods for all of the ContentHandler events. Enter the code highlighted below to extend that class:

public class Echo extends DefaultHandler	
{	
   ...	
}
 

Note: DefaultHandler also defines do-nothing methods for the other major events, defined in the DTDHandler, EntityResolver, and ErrorHandler interfaces. You'll learn more about those methods as we go along.

Each of these methods is required by the interface to throw a SAXException. An exception thrown here is sent back to the parser, which sends it on to the code that invoked the parser. In the current program, that means it winds up back at the Throwable exception handler at the bottom of the main method.

When a start tag or end tag is encountered, the name of the tag is passed as a String to the startElement or endElement method, as appropriate. When a start tag is encountered, any attributes it defines are also passed in an Attributes list. Characters found within the element are passed as an array of characters, along with the number of characters (length) and an offset into the array that points to the first character.

Setting up the Parser

Now (at last) you're ready to set up the parser. Add the text highlighted below to set it up and get it started:

public static void main(String argv[])	
{	
   if (argv.length != 1) {	
      System.err.println("Usage: cmd filename");	
      System.exit(1);	
   }
 
        // Use an instance of ourselves as the SAX event handler	
   DefaultHandler handler = new Echo();
 
        // Use the default (non-validating) parser	
   SAXParserFactory factory = SAXParserFactory.newInstance();	
   try {	
      // Set up output stream	
      out = new OutputStreamWriter(System.out, "UTF8");
 
       // Parse the input 	
      SAXParser saxParser = factory.newSAXParser();	
      saxParser.parse( new File(argv[0]), handler );
 
        } catch (Throwable t) {	
      t.printStackTrace();	
   }	
   System.exit(0);	
}
 

With these lines of code, you created a SAXParserFactory instance, as determined by the setting of the javax.xml.parsers.SAXParserFactory system property. You then got a parser from the factory and gave the parser an instance of this class to handle the parsing events, telling it which input file to process.


Note: The javax.xml.parsers.SAXParser class is a wrapper that defines a number of convenience methods. It wraps the (somewhat-less friendly) org.xml.sax.Parser object. If needed, you can obtain that parser using the SAXParser's getParser() method.

For now, you are simply catching any exception that the parser might throw. You'll learn more about error processing in a later section of the tutorial, Handling Errors with the Nonvalidating Parser.

Writing the Output

The ContentHandler methods throw SAXExceptions but not IOExceptions, which can occur while writing. The SAXException can wrap another exception, though, so it makes sense to do the output in a method that takes care of the exception-handling details. Add the code highlighted below to define an emit method that does that:

static private Writer out;
 
private void emit(String s)	
throws SAXException	
{	
   try {	
      out.write(s);	
      out.flush();	
   } catch (IOException e) {	
      throw new SAXException("I/O error", e);	
   }	
}	
...
 

When emit is called, any I/O error is wrapped in SAXException along with a message that identifies it. That exception is then thrown back to the SAX parser. You'll learn more about SAX exceptions later on. For now, keep in mind that emit is a small method that handles the string output. (You'll see it called a lot in the code ahead.)

Spacing the Output

There is one last bit of infrastructure we need before doing some real processing. Add the code highlighted below to define a nl() method that writes the kind of line-ending character used by the current system:

private void emit(String s) 	
   ...	
}
 
private void nl()	
throws SAXException	
{	
   String lineEnd =  System.getProperty("line.separator");	
   try {	
      out.write(lineEnd);	
   } catch (IOException e) {	
      throw new SAXException("I/O error", e);	
   }	
}
 

Note: Although it seems like a bit of a nuisance, you will be invoking nl() many times in the code ahead. Defining it now will simplify the code later on. It also provides a place to indent the output when we get to that section of the tutorial.

Handling Content Events

Finally, let's write some code that actually processes the ContentHandler events. Add the code highlighted below to handle the start-document and end-document events:

static private Writer out;	
	
public void startDocument()	
throws SAXException	
{	
   emit("<?xml version='1.0' encoding='UTF-8'?>");	
   nl();	
}	
	
public void endDocument()	
throws SAXException	
{	
   try {	
      nl();	
      out.flush();	
   } catch (IOException e) {	
      throw new SAXException("I/O error", e);	
   }	
}
 
private void emit(String s)	
...
 

Here, you are echoing an XML declaration when the parser encounters the start of the document. Since you set up the OutputStreamWriter using the UTF-8 encoding, you include that specification as part of the declaration.


Note: However, the IO classes don't understand the hyphenated encoding names, so you specified "UTF8" rather than "UTF-8".

At the end of the document, you simply put out a final newline and flush the output stream. Not much going on there. Now for the interesting stuff. Add the code highlighted below to process the start-element and end-element events:

public void startElement(String namespaceURI,	
            String sName, // simple name (localName)	
            String qName, // qualified name	
            Attributes attrs)	
throws SAXException	
{	
   String eName = sName; // element name	
   if ("".equals(eName)) eName = qName; // namespaceAware = 
false	
   emit("<"+eName);	
   if (attrs != null) {	
      for (int i = 0; i < attrs.getLength(); i++) {	
         String aName = attrs.getLocalName(i); // Attr name	
         if ("".equals(aName)) aName = attrs.getQName(i);	
         emit("");	
         emit(aName+"=\"+attra.getvalue(i)+"\"");	
      }	
   }	
   emit(">");	
}}
 
public void endElement(String namespaceURI,	
            String sName, // simple name	
            String qName  // qualified name	
            )
 
throws SAXException	
{	
   emit("</"+sName+">");	
}
 
private void emit(String s)	
...
 

With this code, you echoed the element tags, including any attributes defined in the start tag. Note that when the startElement() method is invoked, the simple name ("local name") for elements and attributes could turn out to be the empty string, if namespace processing was not enabled. The code handles that case by using the qualified name whenever the simple name is the empty string.

To finish this version of the program, add the code highlighted below to echo the characters the parser sees:

public void characters(char buf[], int offset, int len)	
throws SAXException	
{	
   String s = new String(buf, offset, len);	
   emit(s);	
}
 
private void emit(String s)	
...
 

Congratulations! You've just written a SAX parser application. The next step is to compile and run it.


Note: To be strictly accurate, the character handler should scan the buffer for ampersand characters ('&');and left-angle bracket characters ('<') and replace them with the strings "&amp;" or "&lt;", as appropriate. You'll find out more about that kind of processing when we discuss entity references in Substituting and Inserting Text.

Compiling and Running the Program

In the Java WSDP, the JAXP libraries are distributed in the directory <JWSDP_HOME>/common/lib. To compile and run the program you created, you'll first need to install the JAXP libraries in the appropriate location. (The location depends on which version of the JVM you are using. See the JAXP release notes at <JWSDP_HOME>/docs/jaxp/ReleaseNotes.html for details.) Then you can execute the following command:

javac Echo.java
 

where javac is a version 1.2 or later Java platform compiler.

To run the program, execute the command below:

java Echo slideSample.xml
 

Checking the Output

The program's output as shown in Echo01-01. Here is part of it, showing some of its weird-looking spacing:

...	
<slideshow title="Sample Slide Show" date="Date of publication" 
author="Yours Truly">	
	
   <slide type="all">	
      <title>Wake up to WonderWidgets!</title>	
   </slide>	
   ...
 

Looking at this output, a number of questions arise. Namely, where is the excess vertical whitespace coming from? And why is it that the elements are indented properly, when the code isn't doing it? We'll answer those questions in a moment. First, though, there are a few points to note about the output:

does not appear in the listing. Comments are ignored by definition, unless you implement a LexicalHandler. You'll see more about that later on in this tutorial.

Identifying the Events

This version of the echo program might be useful for displaying an XML file, but it's not telling you much about what's going on in the parser. The next step is to modify the program so that you see where the spaces and vertical lines are coming from.


Note: The code discussed in this section is in Echo02.java. The output it produces is shown in Echo02-01.

Make the changes highlighted below to identify the events as they occur:

public void startDocument()	
throws SAXException	
{	
   nl();	
   nl(); 	
   emit("START DOCUMENT");	
   nl(); 	
   emit("<?xml version='1.0' encoding='UTF-8'?>");	
   nl();	
}	
	
public void endDocument()	
throws SAXException	
{	
   nl(); emit("END DOCUMENT");	
   try {	
   ...	
}	
	
public void startElement(...)	
throws SAXException	
{	
   nl(); emit("ELEMENT: ");	
   emit("<"+sName);	
   if (attrs != null) {	
      for (int i = 0; i < attrs.getLength(); i++) {	
         emit(" ");	
         
emit(attrs.getName(i)+"=\""+attrs.getValue(i)+"\"");	
         nl(); 	
         emit("   ATTR: ");	
         emit(attrs.getLocalName(i));	
         emit("\"");	
         emit(attrs.getValue(i));	
         emit("\"");	
      }	
   }	
   if (attrs.getLength() > 0) nl();	
   emit(">");	
}
 
public void endElement(...)	
throws SAXException	
{	
   nl(); 	
   emit("END_ELM: ");	
   emit("</"+sName+">");	
}
 
public void characters(char buf[], int offset, int Len)	
throws SAXException	
{ 	
   nl(); emit("CHARS: |"); 	
   String s = new String(buf, offset, Len);	
   emit(s);	
   emit("|");	
}
 

Compile and run this version of the program to produce a more informative output listing. The attributes are now shown one per line, which is nice. But, more importantly, output lines like this one:

  CHARS: |
 
    |
 

show that the characters method is responsible for echoing both the spaces that create the indentation and the multiple newlines that separate the attributes.


Note: The XML specification requires all input line separators to be normalized to a single newline. The newline character is specified as in Java, C, and UNIX systems, but goes by the alias "linefeed" in Windows systems.

Compressing the Output

To make the output more readable, modify the program so that it only outputs characters containing something other than whitespace.


Note: The code discussed in this section is in Echo03.java.

Make the changes shown below to suppress output of characters that are all whitespace:

public void characters(char buf[], int offset, int Len)	
throws SAXException	
{	
   nl(); emit("CHARS: |");	
   nl(); emit("CHARS:   ");	
   String s = new String(buf, offset, Len);	
   emit(s);	
   emit("|");	
   if (!s.trim().equals("")) emit(s);	
}
 

If you run the program now, you will see that you have eliminated the indentation as well, because the indent space is part of the whitespace that precedes the start of an element. Add the code highlighted below to manage the indentation:

static private Writer out;
 
private String indentString = "    "; // Amount to indent	
private int indentLevel = 0;	
	
...
 
public void startElement(...)	
throws SAXException	
{	
   indentLevel++;	
   nl(); emit("ELEMENT: ");	
   ...	
}
 
public void endElement(...)	
throws SAXException	
{	
   nl(); 	
   emit("END_ELM: ");	
   emit("</"+sName+">");	
   indentLevel--;	
}	
...	
private void nl()	
throws SAXException	
{	
   ...	
   try {	
      out.write(lineEnd);	
      for (int i=0; i < indentLevel; i++) 
out.write(indentString);
 
        } catch (IOException e) {	
   ... 	
}
 

This code sets up an indent string, keeps track of the current indent level, and outputs the indent string whenever the nl method is called. If you set the indent string to "", the output will be un-indented (Try it. You'll see why it's worth the work to add the indentation.)

You'll be happy to know that you have reached the end of the "mechanical" code you have to add to the Echo program. From here on, you'll be doing things that give you more insight into how the parser works. The steps you've taken so far, though, have given you a lot of insight into how the parser sees the XML data it processes. It's also given you a helpful debugging tool you can use to see what the parser sees.

Inspecting the Output

The complete output for this version of the program is shown in Echo03-01. Part of that output is shown here:

ELEMENT: <slideshow	
...	
CHARS: 	
CHARS: 	
   ELEMENT: <slide	
   ... 	
   END_ELM: </slide>	
CHARS: 	
CHARS:   
 

Note that the characters method was invoked twice in a row. Inspecting the source file slideSample01.xml shows that there is a comment before the first slide. The first call to characters comes before that comment. The second call comes after. (Later on, you'll see how to be notified when the parser encounters a comment, although in most cases you won't need such notifications.)

Note, too, that the characters method is invoked after the first slide element, as well as before. When you are thinking in terms of hierarchically structured data, that seems odd. After all, you intended for the slideshow element to contain slide elements, not text. Later on, you'll see how to restrict the slideshow element using a DTD. When you do that, the characters method will no longer be invoked.

In the absence of a DTD, though, the parser must assume that any element it sees contains text like that in the first item element of the overview slide:

<item>Why <em>WonderWidgets</em> are great</item>
 

Here, the hierarchical structure looks like this:

ELEMENT: <item>	
CHARS:   Why 	
   ELEMENT: <em>	
   CHARS:   WonderWidgets	
   END_ELM: </em>	
CHARS:    are great	
END_ELM: </item>
 

Documents and Data

In this example, it's clear that there are characters intermixed with the hierarchical structure of the elements. The fact that text can surround elements (or be prevented from doing so with a DTD or schema) helps to explain why you sometimes hear talk about "XML data" and other times hear about "XML documents". XML comfortably handles both structured data and text documents that include markup. The only difference between the two is whether or not text is allowed between the elements.


Note: In an upcoming section of this tutorial, you will work with the ignorableWhitespace method in the ContentHandler interface. This method can only be invoked when a DTD is present. If a DTD specifies that slideshow does not contain text, then all of the whitespace surrounding the slide elements is by definition ignorable. On the other hand, if slideshow can contain text (which must be assumed to be true in the absence of a DTD), then the parser must assume that spaces and lines it sees between the slide elements are significant parts of the document.

Home
TOC
PREV TOP NEXT