Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

XML documents on the run, Part 2

Better SAX2 handling and the pull-parser alternative

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 6

A better interface

The last example from Part 1 employed a simple interface for our own handler classes:

public interface EndElementHandler
{
    public void endElement(String lname, String content);
}


Since that code set the handler directly and used only element content, just one simple method was necessary. Now we want to handle attributes as well as content. We also want to nest handlers -- have one handler pass off control to a separate handler for processing a substructure, like the tracking information in our revised document. Figure 1 shows how this should work when we're processing an option-trade element, for example.

Figure 1. Stackable handlers in action: tracking element within the stock-trade element

We'll need a more complex interface to handle these requirements; it'll need support for attributes and some way to set a nested handler. Here's the definition of the StructureHandler interface we'll use for this purpose:

public interface StructureHandler
{
    // Start of the root element in the structure being handled.
    public void startElement(String lname, Attributes attrs);
    
    // End of the root element in the structure being handled.
    public void endElement(String lname, String content);
    
    // Start of child element in the structure being handled -- this can
    //  invoke a nested handler, by passing back a non-null value.
    public StructureHandler startChild(String lname, Attributes attrs);
    
    // End of child element handled directly.
    public void endDirectChild(String lname, String content);
    
    // End of child element handled by nested handler.
    public void endStructureChild(String lname, StructureHandler handler);
}


The above interface gives us the information necessary for handling our new, more complicated document format. The startElement() method call informs us that we're beginning our handling and gives a convenient hook for any initialization code. The endElement() method call then informs us when we're finished.

The startChild() method supplies the information for a child element start tag, and gives us the choice of handling it directly (by returning null) or invoking a nested handler (by returning the handler instance). If we handle the child directly, we'll get a call to endDirectChild() on the end tag; if we invoke a nested handler, we'll get a call on endStructureChild() on the end tag. If we use the nested handler, we won't be called for anything between the start and end of the child element -- the nested handler will instead be used for any contained children.

Build a base

We want several classes to implement the StructureHandler interface; most won't actually use all the methods, though. To simplify our later code, we can define a simple base class with dummy interface-method implementations. Our other classes can then subclass that base and override only those methods they actually need to use.

Here's the base class implementation:

public class StructureHandlerBase implements StructureHandler
{
    public void startElement(String lname, Attributes attrs) {}
    
    public void endElement(String lname, String content) {}
    
    public StructureHandler startChild(String lname, Attributes attrs) {
        return null;
    }
    
    public void endDirectChild(String lname, String content) {}
    
    public void endStructureChild(String lname, StructureHandler handler) {}
}


Not the most sophisticated code in the world, but it saves us duplicating these dummy methods in classes that don't need them.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Read Mark Johnson's approach to using both SAX (version 1) and DOM in his three-part "Programming XML in Java" (JavaWorld) series: