Você está na página 1de 4

XML Parsing in J2ME

Here is an example for XML parsing in J2ME. In this example, username and password will be passed to the server using http connection and application receives login status. login status will be displayed in an Alert. Before you start your coding you have to add kxml package in your application. 1. download the kxml zip file - http://kxml.objectweb.org/software/downloads/ 2. Right click your project name in the netbeans and select properies 3. select build-> libraries and resources and add the downloaded zip file. ----------------------------< login> < status> < message> login success </message> </status> </login> -----------------------------package ParseExample; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import ParseExample.RSSParser.RSSListener; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; /** * @author test */ public class XmlParseSample extends MIDlet implements ItemCommandListener, RSSListener { private Display display; private TextField username, password; private StringItem login; private Command select; private Form loginForm; private String loginStatus; public XmlParseSample() { display = Display.getDisplay(this); loginForm = new Form("Login"); username = new TextField("Username", "", 12, TextField.ANY); password = new TextField("Password", "", 8, TextField.PASSWORD); // Using StringItem as Button login = new StringItem("", "Login", StringItem.BUTTON); select = new Command("Select", Command.OK, 0); } public void startApp() { loginForm.append(username); loginForm.append(password); loginForm.append(login); login.setDefaultCommand(select); login.setItemCommandListener(this); display.setCurrent(loginForm); } public void pauseApp() {

} public void destroyApp(boolean unconditional) { } public void commandAction(Command cmd, Item item) { if (cmd == select && item == login) { String user = username.getString(); String pass = password.getString(); String url = "http://..........?username=" + user + "&password=" + pass; try{ RSSParser parser = new RSSParser(); parser.setRSSListener(this); } catch (Exception e) { } } } public void itemParsed(String message) { this.loginStatus = message; Alert a = new Alert("Exception", loginStatus, null, null); a.setTimeout(Alert.FOREVER); display.setCurrent(a); } public void exception(java.io.IOException ioe) { Alert a = new Alert("Exception", ioe.toString(), null, null); a.setTimeout(Alert.FOREVER); display.setCurrent(a); } } ----------------------------package ParseExample; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.*; import javax.microedition.io.*; import org.kxml.*; import org.kxml.parser.*; public class RSSParser { XmlParseSample xmlParseSample; protected RSSListener mRSSListener; public void setRSSListener(RSSListener listener){ mRSSListener = listener; } //non blocking public void parse(final String url) { Thread t = new Thread() { public void run() { HttpConnection hc = null;

try { hc = (HttpConnection) Connector.open(url); parse(hc.openInputStream()); } catch (IOException ioe) { mRSSListener.exception(ioe); } finally { try { if (hc != null) { hc.close(); } } catch (IOException ignored) { } } } }; t.start(); } // Blocking. public void parse(InputStream in) throws IOException { Reader reader = new InputStreamReader(in); XmlParser parser = new XmlParser(reader); ParseEvent pe = null; parser.read(Xml.START_TAG, null, "login"); boolean trucking = true; while (trucking) { pe = parser.read(); if (pe.getType() == Xml.START_TAG) { String name = pe.getName(); if (name.equals("status")) { while ((pe.getType() != Xml.END_TAG) || (pe.getName().equals(name) == false)) { pe = parser.read(); if (pe.getType() == Xml.START_TAG && pe.getName().equals("message")) { pe = parser.read(); message = pe.getText(); } } mRSSListener.itemParsed(message); } else{ while ((pe.getType() != Xml.END_TAG) || (pe.getName().equals(name) == false)) { pe = parser.read(); } } } if (pe.getType() == Xml.END_TAG && pe.getName().equals("login")) { trucking = false; } } } public interface RSSListener { public void itemParsed(String message); public void exception(java.io.IOException ioe);

} }

Você também pode gostar