MinML

Team-Fly

MinML implements the SAX 1.0 APIs, but this means that it won't build and run on MIDP without some modification. In this section, I'll describe how to get MinML to build on MIDP. Then we'll take a look at how to use MinML from a MIDlet.

One important limitation of MinML is that it does not support mixed content, an element that contains both character data and other elements. Without mixed content, elements contain either character data or they contain other elements. For example, the following element contains character data:

 <text>excuse me but can I be you for a while</text> 

By contrast, the following "text" element contains both character data and another element, "italic":

 <text>excuse me but can I <italic>be you for a while</italic></text> 

Our approach to making MinML build on MIDP will be to supply missing classes. MinML expects to find two classes, java.util.Locale and java.net.URL, that are not present on the MIDP platform. Rather than modifying the MinML source code (also a valid approach), we will supply dummy implementations of these two classes. First, we can supply an empty implementation for java.util.Locale:

 package java.util; public class Locale { } 

If you're using J2MEWTK for development, simply save this file as Locale.java in the <J2MEWTK>/apps/<projectname>/src/java/util directory. You can follow a similar process for java.net.URL:

 package java.net; import java.io.InputStream; public class URL {   public URL(String url) {}   public InputStream openStream() { return null; } } 

Save this code in <J2MEWTK>/apps/<projectname>/src/java/net. Now MinML should compile without errors.

To actually use the parser, you will most likely extend uk.co.wilson.xml.MinML, which implements the org.xml.sax.Parser interface. In addition, MinML also implements the DocumentHandler interface. You might, therefore, create a MinML instance and override the DocumentHandler methods in which you are interested. The following code illustrates this technique:

 String filename = "example1.xml"; InputStream rawIn = this.getClass().getResourceAsStream(filename); Reader in = new InputStreamReader(rawIn); try {   MinML p = new MinMLSubclass();   p.parse(in); } catch (Exception e) { // Handle exceptions. } 

As the file is parsed, the DocumentHandler methods in your MinMLSubclass instance will be called. If you override DocumentHandler methods like startElement(), characters(), and others, you can get callbacks for the information that you need.


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