TinyXML

Team-Fly

TinyXML also follows the DOM model of parsing an entire document into some internal representation. It will not compile directly on MIDP, however, and requires some modifications to the source code itself. In the gd.xml.XMLParser source code, the following changes will result in MIDP-compliant code:

  1. Create local static methods isLetter() and isLetterOrDigit() as follows:

     private static boolean isLetter(char c) {       return (c >= 'a' && c <= 'z') ||           (c >= 'A' && c <= 'Z') ||          c == ':' ||          c == '_'; } private static boolean isLetterOrDigit(char c) {   return isLetter(c) || (c >= '0' && c <= '9'); } 

  2. Change all calls to Character.isLetter() to call the static method isLetter(). Similarly, change calls to Character.isLetterOrDigit() to isLetterOrDigit().

The gd.xml.XMLReader class also requires some modifications. Replace the setEncoding() method with the following:

 public void setEncoding(String enc) throws UnsupportedEncodingException {   if (enc==null) throw new UnsupportedEncodingException();   else if (enc.equals("ASCII")) enc = ASCII;   else if (enc.equals("UTF-16")) enc = UTF16B;   else if (enc.equals("UTF-16BE")) enc = UTF16B;   else if (enc.equals("UTF-8")) enc = UTF8;   if (enc!=ASCII && enc!=UTF16B && enc!=UTF16L && enc!=UTF8)     throw new UnsupportedEncodingException();   encoding = enc; } 

Finally, in the gd.xml.tiny.TinyParser class, remove references to java.net.URL as follows:

  1. Comment out the import java.net.* line.

  2. Comment out the parseXML(URL url) method.

  3. Comment out the parseXML(String fname) method.

When that is done, TinyXML will build in a MIDP environment. (Christian Sauer followed a similar process to port TinyXML to the CLDC PalmOS implementation. Details are here: http://www.kvmworld.com/Articles/TinyXML.shtml.)

To use TinyXML, create an instance of gd.xml.tiny.TinyParser and call its parseXML() method, as follows:

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

The parseXML() method returns an instance of ParsedXML representing the root of the document. You can browse the elements of the document by calling ParsedXML methods like getName(), getContent(), attributes(), and elements().

Note that TinyXML is picky about its supported encoding types. The supported encoding types are "ASCII," "UTF-16," "UTF-16BE," and "UTF-8." If the encoding specified in the "<?xml … ?>" tag of your document does not match one of these types, the parser will throw an exception and quit.


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