kXML

Team-Fly

kXML is the largest and most complete parser I'll cover. Originally developed at the Universität Dortmund in Germany, it is now part of the Enhydra Web site. It is based on Common XML (http://simonstl.com/articles/cxmlspec.txt), which is a set of recommendations for using XML 1.0. Common XML specifies a core set of XML functionality and is really more of a state of mind than a specification.

kXML is specifically designed for KVM environments like CLDC and MIDP. Of the five parsers covered in this chapter, kXML is the only one that compiles without modification in a MIDP environment. It is also the largest; if you're concerned about memory, you might want to consider one of the other parsers, which are considerably smaller.

kXML implements a pull-based parser that is contained in the de.kxml.parser package. Pull-based means that you tell the parser to parse each element. A SAX parser, by contrast, parses the whole document in one shot and just lets you know when things happen.

Most of kXML's parser functionality is defined in the abstract Parser class. In your code, you will use the concrete DefaultParser subclass. The basic idea is to instantiate a DefaultParser, passing in a java.io.Reader that represents the data to be parsed. Then call the read() method repeatedly, processing each element until the end of the document is reached. read() returns a ParseEvent, which could represent a start tag, element text, or other parser events. In MIDlet code, it looks something like this:

 String filename = "example1.xml"; InputStream rawIn = this.getClass().getResourceAsStream(filename); Reader in = new InputStreamReader(rawIn); try {   Parser p = new DefaultParser(in);   ParseEvent pe = null;   while ((pe = p.read()) != null) {     ; // Process the event.     if (pe.getType() == de.kxml.Xml.END_DOCUMENT)       break;   } } catch (IOException ioe) {   // Handle the exception. } 

ParseEvent has a type property (returned by getType()) that will be one of the constants defined in the de.kxml.Xml class. ParseEvent also has various subclasses that represent some of the event types. However, downcasting will rarely be necessary as ParseEvent already contains most of the methods you need for accessing data.

In the previous sample code, we've explicitly tested for an END_DOCUMENT event type that signals that the parser is finished.

start sidebar
How to Build a Parser

The process for building an XML parser in MIDP is surprisingly easy. I used the Java 2, Micro Edition Wireless Toolkit (J2MEWTK) from Sun. The source code for all the parsers is readily available. All you have to do is copy the source code into one of the J2MEWTK project directories. For example, the MinML parser has two directories of source code, org and uk, containing the SAX source code and the MinML source code, respectively. Using the J2MEWTK, I created a new project called MinMLproject. J2MEWTK created a new directory, <J2MEWTK>/apps/MinMLproject/src. I just copied the MinML source code into this directory. Inside J2MEWTK, I pressed the Build button, which compiled all the source code.

If you make the modifications as described in this chapter, you can get MinML to compile without errors. Then just add some MIDlet code and you're ready to go. J2MEWTK will automatically package the MinML classes with your MIDlet classes to make a MIDlet suite JAR.

end sidebar


Team-Fly


Wireless Java. Developing with J2ME
ColdFusion MX Professional Projects
ISBN: 1590590775
EAN: 2147483647
Year: 2000
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net