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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Kxml Parser Example 
 

In this example we use "catching parsing events" way to parse the xml.

 

Kxml Parser Example

                         

In this example we use "catching parsing events" way to parse the xml. Through 'catching parsing events', the parser traverses the XML data and issues callbacks to a previously registered event listener whenever it encounters particular structures in the data. For example, when the parser encounters an opening tag such as <employee> then the event listener would receive an event notifying it of the encounter and pass it any necessary information. A parser implements such a strategy is called a push parser because the parser is "pushing" the event to a listener. 

The Entire Application is as follows:

Program console screen shot:

Full code of the application 

KXMLParserExample.java

import java.io.*;
import org.kxml.*;
import org.kxml.parser.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class KXMLParserExample extends MIDlet {
  public String emp = "/employee.xml";
  XmlParser parser = null;

  protected void startApp(){
    try{
      parser = new XmlParser(new 
      InputStreamReader
(this.getClass().getResourceAsStream(emp))300);

      ParseEvent event = null;
      while((event = parser.read()).getType() != Xml.END_DOCUMENT){
        if(event.getType() == Xml.START_TAG){
          String name = event.getName();
          if(name != null && name.equals("details")){
            System.out.println("----------[ EMP-DETAILS ]----------");
            parseAddressTagparser );
            System.out.println("-----------------------------------\n");
          }
          name = null;
        }
        event = null;
      }
    }catch (IOException ioe){
      System.out.println("XML Parsing Error: " + ioe);
      ioe.printStackTrace();
    }finally{
      parser = null;
    }      
  }

  protected void pauseApp(){}
  
  public void parseAddressTagXmlParser parser throws IOException {
    ParseEvent event = null;
    while ((event = parser.peek()).getType() != Xml.END_DOCUMENT) {
      String name = event.getName();
      int type = event.getType();
      if (type == Xml.END_TAG && name.equals("details")) {
        event = null;
        name = null;
        return;
      }
      event = parser.read();
      if (type != Xml.START_TAG) {
        event = null;
        continue;
      }
      ParseEvent next = parser.read();
      if (next.getType() != Xml.TEXT) {
        event = null;
        next = null;
        continue;
      }
      String text = next.getText();
      System.out.printlnname + ": " + text );
      event = null;
      text = null;
      next = null;
    }
  }

  protected void destroyApp(boolean arg){
    notifyDestroyed();
  }
}

employee.xml

<employee>

    <details>
        <name>Sandeep Kumar Suman</name>
        <designation>J2ME Programmer</designation>
        <city>Gorakhpur</city>
        <state>UP</state>
    </details>

</employee>

Download Source Code of the application discussed above.

                         

» 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.