Java Reference
In-Depth Information
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) {
}
}
TinyXML
TinyXML was designed by Tom Gibara to be used in applets and other situations in which code size is
important. TinyXML has been ported to CLDC by Christian Sauer. Detailed information about the port
is available at http://www.microjava.com/news/techtalk/tinyxml . In contrast to NanoXML, TinyXML
provides a callback interface similar to the Simple Access Interface to XML (SAX), the de facto Java
parsing standard. In order to parse an XML document, you need to create an XMLParser object,
register a DocumentHandler using the setDocumentHandler() method, and assign an
XMLInputStream using setInputStream() . The DocumentHandler provides the callback
interface mentioned earlier. It contains methods such as elementStart() , charData() , and
elementEnd() , which are called when the parser encounters the corresponding XML code.
You will again use Newsforge XML to build an example application (see Listing 10.3 ) . Unfortunately,
you cannot just use any InputStream or Reader with TinyXML; you must provide an
XMLInputStream that is constructed from a String . Thus, you need to build a string from the
InputStream obtained from the HttpConnection before you can invoke the parser.
Listing 10.3 TinyNewsreader.java —TinyXML Newsreader Example
import java.io.*;
import java.util.Vector;
import java.util.Hashtable;
import tinyxml.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class TinyNewsreader extends MIDlet implements CommandListener
{
static final String URL = "http://newsforge.com/newsforge.xml";
static final String TITLE = "NewsForge";
Vector descriptions = new Vector();
List newsList = new List (TITLE, Choice.IMPLICIT);
TextBox textBox = new TextBox ("", "", 256, TextField.ANY);
Display display;
Command backCmd = new Command ("Back", Command.BACK, 0);
class NewsHandler extends HandlerBase {
StringBuffer title;
StringBuffer description;
String currentElement = "";
 
Search WWH ::




Custom Search