Java Reference
In-Depth Information
For that reason, many implementers prefer to have a complete object tree such as the NanoXML
kXMLElement structure before actually processing an XML document.
A pull parser such as kXML works similar to a reader, where the application is in control of reading
data. The advantage is that parsing can be performed in recursive functions, following the tree structure
of the document. Instead of having an explicit global state object, the program state reflects the parsing
state in a natural way.
The kXML pull parser is implemented in the XmlParser object. The next parse event can be queried
with the peek() method or read with the read() method. Both methods return a ParseEvent
object. ParseEvent provides access methods such as getType() , getName() , and getText() .
The getType() method returns the type of the event—for example, Xml.START_TAG ,
Xml.END_TAG , or Xml.TEXT . The getName() method returns the name of the element if the
event is an element start or element end event. The corresponding namespace can be obtained with
getNamespace() . For text events, getText() returns the corresponding text strings. The parser
also provides some convenience methods. For example, XmlParser.skip() skips over events that
are ignorable in most cases, such as whitespace or comments. Special versions of peek() and read()
also test for a certain type of event. For example, peek(Xml.START_TAG, null, "story")
returns true if the next event is a start tag with the name story in any namespace.
As you did for the other two parsers, you will implement an MIDP client for the Newsforge XML
service (see Listing 10.4 ) . kXML accepts any reader as a source for the XML input, so you just need to
wrap an InputStreamReader around the InputStream obtained from the HttpConnection .
This feature enables you to begin processing the XML code while still receiving data. The advantage is
that you can display the first news titles while still reading data from the server, reducing the time users
need to wait before they can start reading. In order to use this feature, you just need to put the parsing
process in a separate thread. Note that this approach would make sense for the TinyXML example, too,
if the CLDC port would accept a regular InputStream for reading.
Note
This section handles kXML version 1.x. Version 2.0 will have a slightly different interface, similar to
the XML parser available from kobjects.org . The kobjects parser is a pull parser as well, but it does not
support XML namespaces.
Listing 10.4 KxmlNewsreader.java —kXML Newsreader Example
import java.io.*;
import java.util.Vector;
import org.kxml.*;
import org.kxml.parser.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class KxmlNewsreader 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);
 
Search WWH ::




Custom Search