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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
J2ME Kxml Connection Example 
 

This is the XML connection example. In this section, we'll develop a J2ME application that a user can connect to the xml file from localhost and print it on the console.

 

J2ME Kxml Connection Example

                         

This is the XML connection example. In this section, we'll develop a J2ME application that a user can connect to the xml file from localhost and print it on the console.

To develop this type of application we have the following steps:

  • create a AddressBook.xml file in root folder of your server with name and address tags.
  • run the server by startup.bat
  • create the MIDlet (AddressDB.java).
  • build the midlet and then run.
  • approve to send and receive the data using network.
  • output display on console.  

AddressBook is a simple XML format that summarizes name and address tags. when the program will executed,AddressBook displays the name and address from the addressbook data on the console.

 

The Entire Application is as follows:

 

 

AddressDB.java

import java.io.*;

import org.kxml.*;
import org.kxml.io.*;
import org.kxml.kdom.*;
import org.kxml.parser.*;

import javax.microedition.io.*;
import javax.microedition.rms.*;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class AddressDB extends MIDlet{
  private static RecordStore rs = null;

  public void startApp(){
    try {
      rs = RecordStore.openRecordStore("addressbook"true);
      loadXML();
      addAddress("Sandeep Kumar Suman""Gorakhpur");
      getName(1);
      getAddress(1);
    catch (RecordStoreException e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }

  public void addAddress(String Name, String Address)  {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DataOutputStream output = new DataOutputStream(os);

    try {
      output.writeUTF(Name + "," + Address);
    catch (IOException e) {
      System.out.println(e);
      e.printStackTrace();
    }
    byte[] b = os.toByteArray();
    try {
      rs.addRecord(b, 0, b.length);
    catch (RecordStoreException e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }

  public static String getName(int index)  {
    int counter = 1;
    int commalocation = 0;
    String name = null;
    try {
      RecordEnumeration enumRec =  rs.enumerateRecords(null, null, false);
      while ((counter <= index&& (enumRec.hasNextElement()))   {
        String strTemp = new String(enumRec.nextRecord());
        commalocation = strTemp.indexOf(',');
        name = strTemp.substring(2, commalocation);
        counter++;
      }
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
    System.out.println("Name::::::::"+name);
    return name;    
  }

  public static String getAddress(int index)  {
    int counter = 1;
    int commalocation = 0;
    String address = null;

    try {
      RecordEnumeration enumRec = rs.enumerateRecords(null, null, false);
      while ((counter <= index&& (enumRec.hasNextElement())){
        String strTemp = new String(enumRec.nextRecord());
        commalocation = strTemp.indexOf(',');
        address = strTemp.substring(commalocation + 1);
        counter++;
      }
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
    System.out.println("Address::::::::"+address);
    return address;
  }

  public static int recordCount(){
    int count = 0;
    try  {
      count = rs.getNumRecords();
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
    return count;
  }

  public void loadXML()  {
    StreamConnection c = null;
    InputStream s = null;
    Attribute att = null;
    String Name = null;
    String Address = null;
    try {
      rs.closeRecordStore();
      rs.deleteRecordStore("addressbook");
      rs = RecordStore.openRecordStore("addressbook"true);
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
    try {
      c = (StreamConnection)Connector.open("http://localhost:8080/AddressBook.xml");
      s = c.openInputStream();

      XmlParser parser = new XmlParser(new InputStreamReader(s));
      Document document = new Document();
      document.parse(parser);
      System.out.println(document);
      Element root = document.getRootElement();
      int children = root.getChildCount();
      
      for(int i=0; i < children; i++) {
        if (root.getType(i== Xml.ELEMENT) {
          Element el = root.getElement(i);
          int babies = el.getChildCount();
          for(int j=0; j < babies; j++){
            if (el.getType(j== Xml.ELEMENT) {
              Element elName = (Element)el.getChild(j);
              if (elName.getName().equals("name")){
                att = elName.getAttribute("value");
                Name = att.getValue();
              else if (elName.getName().equals("address")){
                att = elName.getAttribute("value");
                Address = att.getValue();
              }
            }
          }
          addAddress(Name, Address);
        }
      }
    catch (IOException err) {
      System.out.println(err);
      err.printStackTrace();
    }
  }
    
  public void pauseApp(){}
  
  public void destroyApp(boolean unconditional){
    notifyDestroyed();
  }
}

 

AddressBook.xml

<?xml version="1.0"?>

<addressbook >

    <name>Sandeep Kumar Suman</name> 
   <address>Gorakhpur</address> 

</addressbook>

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.