Java Reference
In-Depth Information
to a specific parser class written to parse XML with specific tags. Pull parsers work the
opposite way; the code scanning the XML invokes the XML parser's methods to obtain
individual tags and other information. As you see in subsequent sections of this chap-
ter, both the push and pull models are available for Java ME, and which you use is
primarily a decision based on the availability of the parser you want to use. In prac-
tice, I think it's fair to say that a pull parser may use slightly less memory than a push
parser; however, given one, you can easily code the other (as I show later in this chap-
ter), so they're essentially equivalent in all but name and how you structure the code
that interacts with them.
Generating XML in Java ME Applications
Generating XML in a Java ME application is a simple, if tedious, task, using the
StringBuffer class. You simply build the XML document, tag by tag, using a combination
of compile-time strings for the tags and values of your application's objects for the tag
and attribute values. Listing 13-7 shows a revised version of the WeatherWidget's Location
class that supports the additional weather information that an XML document such as
the one in Listing 13-4 might bear, including a toXml method that creates an XML repre-
sentation of the Location class.
Listing 13-7. A Version of the Location Class That Can Generate an XML Representation of
Its Value
package com.apress.rischpater.weatherwidget;
import javax.microedition.rms.*;
import java.io.*;
public class Location {
private final static int FIELD_VERSION = 1;
private final static int FIELD_CITY = 2;
private final static int FIELD_FORECAST = 3;
private final static int FIELD_TEMP = 4;
private final static int FIELD_WINDSPEED = 5;
private final static int FIELD_WINDDIR = 6;
private final static int FIELD_PRECIP = 7;
private final static int FIELD_STATE = 8;
private final static int FIELD_COUNTRY = 9;
private final static int FIELD_PRECIP_PROB = 10;
private final static int FIELD_PRECIP_TYPE = 11;
 
Search WWH ::




Custom Search