Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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.getDisplaythis );
    exit = new Command"Exit", Command.EXIT, );
    xml  = new Command"XML", Command.SCREEN, );
    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);
    t.addCommandexit );
    t.addCommandxml );
    t.setCommandListenerthis );
    display.setCurrent);
  }

  public void pauseApp() { }
  
  public void destroyApp(boolean unconditional) { }

  public void commandAction(Command c, Displayable s) {
    if c == exit ) {
      destroyAppfalse );
      notifyDestroyed();
    else if c == xml ) {
      try {
        viewXML();
        t.removeCommand(xml);
      catchException e ) {
        e.printStackTrace();
      }
    }
  }

  public void viewXML() throws IOException {
    try {
      byte[] xmlByteArray = xmlStr.getBytes();
      ByteArrayInputStream xmlStream = new ByteArrayInputStreamxmlByteArray );
      InputStreamReader xmlReader = new InputStreamReaderxmlStream );
      XmlParser parser = new XmlParserxmlReader );
      try {
        traverseparser, "" );
      catch (Exception exc) {
        exc.printStackTrace();
      }
      return;
    catch IOException e ) {
      return ;
    finally {
    return ;
    }
  }

  public static void traverseXmlParser 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;
          traverseparser, "" ;
        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.setStringtextBoxString );
  }
}

Download Source Code

                         

» View all related tutorials
Related Tags: c xml events parsing list orm data form event io struct parse count parser format callbacks gis callback vi structure

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.