Home J2me Kxml J2ME Kxml Example
Questions:Ask|Latest

Related Tutorials


 
 

Share on Google+Share on Google+

J2ME Kxml Example

Advertisement
This is the simple kxml application, In this application we'll show you how to work with kxml parser and how to parse the xml file in the midlet .

J2ME Kxml Example

     

This is the simple kxml application, In this application we'll show you how to work with kxml parser and how to parse the xml file in the midlet . 

In this section we take the following steps:

  • We  create an employee.xml file, in which we have four tags that is : name, city, state and designation. 
  • We  use the XmlParser in our MIDlet to access the value of xml file and print on the j2me console. 
  • We manipulates the DOM to parse the xml file.

Manipulating the DOM is a simple way of interacting with XML, where the entire XML tree is parsed into a node structure. The XML tree  resides in memory and you can traverse the tree programmatically.

 

The Entire Application is as follows:

 

Source code of KXMLExample.java

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

public class KXMLExample extends MIDlet {
  XmlParser parser = null;
  Document doc = new Document();
  public String emp = "/employee.xml";

  protected void startApp(){
  try {
  InputStream in = this.getClass().getResourceAsStream(emp);
  InputStreamReader is = new InputStreamReader(in);
  parser = new XmlParseris );
  doc.parseparser );
  parser = null;
  catch (IOException ioe) {
  System.err.println(ioe);
  ioe.printStackTrace();
  parser = null;
  doc = null;
  return;
  
  Element root = doc.getRootElement();
  int child_count = root.getChildCount();

  for (int i = 0; i < child_count ; i++ ) {
  if (root.getType(i!= Xml.ELEMENT) {
  continue;
  }

  Element element = root.getElement(i);
  if (!element.getName().equals("details")) {
  continue;
  }

  System.out.println("----------[ EMP-DETAILS ]----------");
  int address_item_count = element.getChildCount();

  for (int j = 0; j < address_item_count ; j++) {
  if (element.getType(j!= Xml.ELEMENT) {
  continue;
  }
  Element item = element.getElement(j);
  System.out.printlnitem.getName() ": " + item.getText(0));
  item = null;
  }
  System.out.println("-----------------------------------\n");
  element = null;
  }  
  }

  protected void pauseApp(){}

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

Output:

The Complete Source Code Here: Download

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2008

Related Tutorials

Ask Questions?    Discuss: J2ME Kxml Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DK Patel
September 26, 2011
KXML

please provide a brief discription of the program how it works.....
DMCA.com