KXML Parse Example

In this section, we will develop a J2ME application to parses the RSS
document and then display the data on the mobile screen. RSS (rich site summary) is a simple XML format, that
summarizes headlines and story descriptions which is published on the website.
You can use this application to develop mobile applications such as RSS news
reader or a simple news reader applications for J2ME enabled mobile devices.
Note: This example does not reads the physical xml file, instead we have constructed
the xml string in the program itself. It is also possible to directly read the
xml from website and process it.
Normal DOM parsers can't be used in the mobile devices. We need a simple and
light weight api to parse the xml files. The KXML api is specially developed for
mobile devices and can be used to develop mobile applications. To parse the xml
file in mobile application, KXML api can be used. It is distributed from http://kxml.sourceforge.net/.
To use KXML api in your program download it from the above website and include
the zip file into applications library (e.g. copy to lib folder).
In the Example there a text box and a button "XML". On startup of
the application "kXML" is displayed in the text box. After pressing
the "XML" button program begins to proceeds and parses the xml data
and displays into text box (mobile screen).
The Entire Application is as follows:
On application startup:

After pressing the "XML" button:

Program code:
import java.io.*;
import org.kxml.*;
import org.kxml.parser.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.Displayable;
public class ParseXML extends MIDlet implements CommandListener {
private Command exit;
private Command xml;
private Display display;
private static TextBox t;
private static String textBoxString = "";
private String xmlStr = "";
public ParseXML() {
display = Display.getDisplay( this );
exit = new Command( "Exit", Command.EXIT, 2 );
xml = new Command( "XML", Command.SCREEN, 1 );
StringBuffer xmlString = new StringBuffer();
xmlString.append("<?xml version=\"1.0\"?> <!DOCTYPE rss PUBLIC \"-//Netscape
Communications//DTD RSS 0.91//EN\"");
xmlString.append("\"http://my.netscape.com/publish/formats/rss-0.91.dtd\">");
xmlString.append("<rss version=\"0.91\">");
xmlString.append("<channel><title>RoseIndia Technology: A Leading IT Company</title>");
xmlString.append("<link>http://roseindia.net</link>");
xmlString.append("<description>RoseIndia provide Web-based it solution..");
xmlString.append("</description><language>en-us</language>");
xmlString.append("</channel>");
xmlString.append("</rss>");
xmlStr = xmlString.toString();
}
public void startApp() {
t = new TextBox( "MIDlet XML", "kXML", 256, 0 );
t.addCommand( exit );
t.addCommand( xml );
t.setCommandListener( this );
display.setCurrent( t );
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable s) {
if ( c == exit ) {
destroyApp( false );
notifyDestroyed();
} else if ( c == xml ) {
try {
viewXML();
t.removeCommand(xml);
} catch( Exception e ) {
e.printStackTrace();
}
}
}
public void viewXML() throws IOException {
try {
byte[] xmlByteArray = xmlStr.getBytes();
ByteArrayInputStream xmlStream = new ByteArrayInputStream( xmlByteArray );
InputStreamReader xmlReader = new InputStreamReader( xmlStream );
XmlParser parser = new XmlParser( xmlReader );
try {
traverse( parser, "" );
} catch (Exception exc) {
exc.printStackTrace();
}
return;
} catch ( IOException e ) {
return ;
} finally {
return ;
}
}
public static void traverse( XmlParser parser, String indent ) throws Exception{
boolean leave = false;
String title = new String();
String desc = new String();
do {
ParseEvent event = parser.read ();
ParseEvent pe;
switch ( event.getType() ) {
case Xml.START_TAG:
if ("title".equals(event.getName())){
pe = parser.read();
title = pe.getText();
}
if ("description".equals(event.getName())){
pe = parser.read();
desc = pe.getText();
}
textBoxString = title + " " + desc;
traverse( parser, "" ) ;
break;
case Xml.END_TAG:
leave = true;
break;
case Xml.END_DOCUMENT:
leave = true;
break;
case Xml.TEXT:
break;
case Xml.WHITESPACE:
break;
default:
}
} while( !leave );
t.setString( textBoxString );
}
}
|
Download Source Code

|