[Previous] [Contents] [Next]


The Interfaces and Classes in the SAX Distribution


Part of these interfaces are aimed at parser writers. We will not cover these.

Others are aimed at application writers.

These interfaces are

DocumentHandler
ErrorHandler
DTDHandler
EntityResolver

Simple XML applications can already be developed by using only DocumentHandler and possibly ErrorHandler.

The following methods are the most important ones defined in the DocumentHandler interface:

startDocument()-To receive notification of the beginning of a document
endDocument()-To receive notification of the end of a document
startElement(String name, AttributeList atts)-To receive notification of the beginning of an element, as shown in Table 15.2
endElement(String name)-To receive notification of the end of an element, as shown in Table 15.3
characters(char ch[], int start, int length)-to receive notification of character data, as shown in Table 15.4
processingInstruction (String target, String data)-To receive notification of a processing instruction, as shown in Table 15.5
Table 15.3 Parameters of startElement

Name Content Type

name The element name String
atts The attributes attached to the element AttributeList

AttributeList allows you to iterate an attribute list.
Table 15.4 Parameter of endElement

Name Content Type

name The element name String

Table 15.5 Parameters of Characters

Name Content Type

ch The characters from the XML doc Array of char
start The start position in the array Integer
length The number of characters to read from the array Integer



Table 15.6 Parameters of processingInstruction

Name Content Type

target The PI's target String
data The PI's data String

Note the similarity with the event handlers of Omnimark.

The SAX package also offers a HandlerBase class, which provides default implementations of the four interfaces mentioned in the preceding list.

Application writers have two possibilities now:

They can define a class that implements the needed interfaces
They can subclass the class HandlerBase-it already has default implementations of the interfaces

A subclass enables us to inherit variables (properties) and methods (behavior) from its superclass but also to override the methods.

A subclass is a class that is a special case of another class.

Listing 15.21 shows our subclassing of the class HandlerBase.

Listing 15.21 Subclassing the Class HandlerBase


1:  public class OurHandler extends HandlerBase { //subclassing
HandlerBase
2:
3:       public void startDocument ()
4:             {
5:   //our implementation of the method, overriding  HandlerBase
6:   }
7:
8:   public void endDocument ()
9:              {
10:                     //our implementation of the method, overriding
HandlerBase
11:             }
12:
13:       public void startElement (String name, AttributeList atts)
14:            {
15:                     //our implementation of the method, overriding
HandlerBase
16:            }
17:
18:       public void endElement (String name)
19:            {
20:                     //our implementation of the method, overriding
HandlerBase
21:             }
22:
23:       public void characters (char ch[], int start, int length)
24:             {
25:                     //our implementation of the method, overriding
HandlerBase
26:             }
27:  }

The main methods defined in the DocumentHandler interface are implemented on lines 3, 8, 13, 18, and 23.

[Previous] [Contents] [Next]