Java Reference
In-Depth Information
L ISTING 10.3
Continued
// Put the element/value pair into the Hashtable
table.put(currentElement, currentValue);
}
}
}
As you look over this handler, you will notice that there are only five methods, two of which
are only accessors to a Hashtable . The other three methods represent the events you are inter-
ested in responding to. Each of these methods will be discussed in the following sections. The
first method overridden is startElement() , which is shown here:
// Called when a new element is encountered
public void startElement(String tag, AttributeList attrs)
throws SAXException {
// hold onto the new element tag, that will be placed in
// our Hashtable when matching character data is encountered.
currentElement = tag;
}
This method is called whenever the parser encounters a new element in the XML document. A
new element would be a starting tag similar to <ID> . When your overridden method is called,
you simply hold onto the passed-in tag representing the element name.
The next method you override is the characters() method. The overridden method is shown
here:
// Called when character data is found inside an element
public void characters(char[] ch, int start, int length)
throws SAXException {
// create a String containing the characters
// found in the element
currentValue = new String(ch, start, length);
}
This method is invoked when the parser encounters character data inside an element. An exam-
ple of this would be the value 33445 found in the element <ID>33445</ID> . When your over-
ridden method is called, you create a string from the character array and hold onto the string
for later use.
10
The last method you override from the HandlerBase class is the endElement() method, which
is included in the following code snippet:
 
Search WWH ::




Custom Search