Parsing xml data from the server using kxml2 in J2ME - TechRepublic
Question
August 26, 2008 at 09:08 AM
ninkotun

Parsing xml data from the server using kxml2 in J2ME

by ninkotun . Updated 17 years, 10 months ago

Hey members i have an xml file i would love to parse.Am developing a J2ME application that should receive response from the server which is xml.am supposed to parse this data so that it sis displayed on a screen.This is the data






Outband





Request
IMApp01#12345@NOK5110





wv:test3@dsmagic.com


http://206.226.10.25:80/IMPSAPP

test
120
im.user.com#20011224#328746293





and this is what i had done bt i was getting an error

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.IOException;
import java.io.InputStreamReader;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import org.kxml2.io.*;
import org.xmlpull.v1.*;
/**
* @author dninsiima
*/
public class LoginResponse extends MIDlet implements CommandListener{
Form mainForm = new Form (“Login Response from Baraza”);

//Location of xml file
static final String URL = “http://localhost:8080/examples/login.xml”;
Vector ResponseVector = new Vector();
StringItem resultItem = new StringItem (“”, “”);

private final static Command xmlCommand = new Command(“Get XML Data”, Command.OK,1);

class ReadXML extends Thread {

public void run() {
try {
//Open http connection
HttpConnection httpConnection = (HttpConnection) Connector.open(URL);

//Initilialize XML parser
KXmlParser parser = new KXmlParser();

parser.setInput(new InputStreamReader(httpConnection.openInputStream()));

parser.nextTag();

parser.require(XmlPullParser.START_TAG, null, “WV-CSP-Message”);

//Iterate through our XML file
while (parser.nextTag () != XmlPullParser.END_TAG)
readXMLData(parser);

parser.require(XmlPullParser.END_TAG, null, “WV-CSP-Message”);
parser.next();

parser.require(XmlPullParser.END_DOCUMENT, null, null);

}
catch (Exception e) {
e.printStackTrace ();
resultItem.setLabel (“Error:”);
resultItem.setText (e.toString ());

}
}

}

private boolean midletPaused = false;

//
//

/**
* The LoginResponse constructor.
*/
public LoginResponse() {
mainForm.append (resultItem);
mainForm.addCommand (xmlCommand);
mainForm.setCommandListener (this);
}

//
//

//
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the startMIDlet method.
*/
private void initialize() {
// write pre-initialize user code here

// write post-initialize user code here
}
//

//
/**
* Performs an action assigned to the Mobile Device – MIDlet Started point.
*/
public void startMIDlet() {
// write pre-action user code here

// write post-action user code here
}
//

//
/**
* Performs an action assigned to the Mobile Device – MIDlet Resumed point.
*/
public void resumeMIDlet() {
// write pre-action user code here

// write post-action user code here
}
//

//
/**
* Switches a current displayable in a display. The display instance is taken from getDisplay method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if null, then nextDisplayable is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//

/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}

/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}

/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
Display.getDisplay (this).setCurrent (mainForm);
new ReadXML().start();
}

/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}

/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s) {
StringBuffer sb = new StringBuffer() ;
if (c == xmlCommand) {

//Display parsed XML file
for(int i= 0 ; i< ResponseVector.size() ;i++){ Login login = (Login) ResponseVector.elementAt(i); sb.append("\n"); sb.append("Session Type : "); sb.append(login.getSessionType()); sb.append("\n"); sb.append("Transaction Mode : "); sb.append(login.getTransactionMode()); sb.append("\n"); sb.append("Transaction ID : "); sb.append(login.getTransactionID()); sb.append("\n"); sb.append("User ID : "); sb.append(login.getUserID()); sb.append("\n"); sb.append("URL : "); sb.append(login.getURL()); sb.append("\n"); sb.append("Password : "); sb.append(login.getPassword()); sb.append("\n"); sb.append("Time To Live : "); sb.append(login.getTimeToLive()); sb.append("\n"); sb.append("Session Cookie : "); sb.append(login.getSessionCookie()); } resultItem.setLabel("Login Information"); } resultItem.setText(sb.toString()); } private void readXMLData(KXmlParser parser) throws IOException, XmlPullParserException { //Parse our XML file parser.require(XmlPullParser.START_TAG, null, "Session"); Login login = new Login(); while (parser.nextTag() != XmlPullParser.END_TAG) { parser.require(XmlPullParser.START_TAG, null, null); String SessionType = parser.getName(); String text = parser.nextText(); System.out.println ("<"+SessionType+">“+text);

if (SessionType.equals(“URL”))
login.setURL(text);
else if (SessionType.equals(“SessionType”))
login.setSessionType(text);
else if (SessionType.equals(“TransactionMode”))
login.setTransactionMode(text);
else if (SessionType.equals(“TransactionID”))
login.setUserID(text);
else if (SessionType.equals(“UserID”))
login.setPassword(text);
else if (SessionType.equals(“TimeToLive”))
login.setTimeToLive(text);
else if (SessionType.equals(“SessionCookie”))
login.setSessionCookie(text);

parser.require(XmlPullParser.END_TAG, null, “SessionType”);
ResponseVector.addElement(login);

parser.require(XmlPullParser.END_TAG, null, “Session”);
}

}

}

please help me

This discussion is locked

All Comments