Java Reference
In-Depth Information
display = Display.getDisplay (this);
display.setCurrent (newsList);
newsList.setCommandListener(this);
textBox.setCommandListener(this);
textBox.addCommand (backCmd);
if (descriptions.size() == 0) readNews();
}
public void pauseApp() {
}
public void commandAction (Command c, Displayable d) {
if (c == List.SELECT_COMMAND) {
String text = (String) descriptions.elementAt
(newsList.getSelectedIndex());
if (textBox.getMaxSize() < text.length())
textBox.setMaxSize (text.length());
textBox.setString (text);
display.setCurrent (textBox);
}
else if (c == backCmd)
display.setCurrent (newsList);
}
public void destroyApp (boolean really) {
}
}
The most interesting part of the example is the inner class NewsHandler . Instead of implementing
the full DocumentHandler interface, you derive your class from the convenience class
HandlerBase , which provides empty implementations for all DocumentHandler methods. Thus,
you only need to implement the callback methods for events you are actually interested in. For parsers
with a callback (push) interface, you need some representation of the current state in order to know
what to do with the incoming events. For that purpose, you save the name of the element in
currentElement when the start of an element is indicated by a call to elementStart() . If a
new <story> element starts, the title and description buffer variables are initialized. When a text
event is received, it is ignored or appended to the title or description, depending on the current element.
Note that the implemented handling of the currentElement name isn't sufficient for nested
structures; it works only for elements that have no further sub-elements. However, in this case it is
sufficient. Finally, when an element end is detected and the name of the element is story, the title and
description are appended to the corresponding lists.
kXML
kXML is larger than NanoXML and TinyXML, but it is the only XML parser for CLDC that provides
XML namespace support. In addition, optional kDOM and WBXML packages are available for kXML.
In contrast to the other parsers, kXML is a pull-based parser. The motivation for pull parsers is to
provide an easier handling mechanism than a centralized callback interface without needing to build an
explicit XML memory structure. Push parsers such as TinyXML; push all XML events to a few
centralized callback methods. Inside the callback methods, the application needs to look up its internal
state before being able to handle the event correctly. For the Newsforge XML example, it is relatively
simple to keep track of the state; but for highly nested structures, doing so becomes quite a problem.
Search WWH ::




Custom Search