package kxmltest;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
public class HttpServletXml3 extends MIDlet implements CommandListener{
private Display dis;
private Form frm=new Form("网页信息为"); //记住new
private TextField tf=new TextField("输入地址","",50,TextField.ANY);
private Command submit=new Command("登陆",Command.SCREEN,1);
public HttpServletXml3() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
dis=Display.getDisplay(this);
dis.setCurrent(frm);
frm.addCommand(submit);
frm.append(tf);
frm.setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
if(c==submit){
ThreadDemo td=new ThreadDemo();
td.start();
}
}
class ThreadDemo extends Thread{
public void run(){
String xml="<elements>"
+"<text><aa>abcd</aa></text>"
+"<text>text2</text>"
+"<abc>a</abc>"
+"<abc>b</abc>"
+"</elements>";
try {
ByteArrayInputStream bin = new ByteArrayInputStream( xml.getBytes() );
KXmlParser parser = new KXmlParser();
parser.setInput(new InputStreamReader(bin));
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
int eventType=parser.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT){
if(eventType==XmlPullParser.START_DOCUMENT){
System.out.println("start");
}else if(eventType==XmlPullParser.END_DOCUMENT){
System.out.println("end");
}else if(eventType==XmlPullParser.START_TAG){
System.out.println(parser.getName()+" START_TAG");
// System.out.println("start count"+parser.getAttributeCount());
// int size=parser.getAttributeCount();
// for(int i=0;i<size;i++){
// System.out.println(parser.getAttributeName(i)+" getAttributeName(i)");
// System.out.println(parser.getAttributeValue(i)+" getAttributeValue()");
// }
}else if(eventType==XmlPullParser.END_TAG){
System.out.println(parser.getName()+" END_TAG");
}else if(eventType==XmlPullParser.TEXT){
System.out.println(parser.getText()+" getText()");
}
eventType=parser.next();
}
// start
// elements START_TAG
// start count0
// text START_TAG
// start count0
// aa START_TAG
// start count0
// abcd getText()
// aa END_TAG
// text END_TAG
// text START_TAG
// start count0
// text2 getText()
// text END_TAG
// abc START_TAG
// start count0
// a getText()
// abc END_TAG
// abc START_TAG
// start count0
// b getText()
// abc END_TAG
// elements END_TAG
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2
package kxmltest;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
public class HttpServletXml extends MIDlet implements CommandListener{
private Display dis;
private Form frm=new Form("网页信息为"); //记住new
private TextField tf=new TextField("输入地址","",50,TextField.ANY);
private Command submit=new Command("登陆",Command.SCREEN,1);
public HttpServletXml() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
dis=Display.getDisplay(this);
dis.setCurrent(frm);
frm.addCommand(submit);
frm.append(tf);
frm.setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
if(c==submit){
ThreadDemo td=new ThreadDemo();
td.start();
}
}
class ThreadDemo extends Thread{
public void run(){
String xml="<elements>"
+"<text>text1</text>"
+"<text>text2</text>"
+"</elements>";
try {
ByteArrayInputStream bin = new ByteArrayInputStream( xml.getBytes() );
KXmlParser parser = new KXmlParser();
parser.setInput(new InputStreamReader(bin));
System.out.println(XmlPullParser.START_TAG); //2
System.out.println(XmlPullParser.END_TAG); //3
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "elements");
while(parser.nextTag() == XmlPullParser.START_TAG) {
parser.require(XmlPullParser.START_TAG, null, "text");
// handle element content
System.out.println("text content: "+ parser.nextText());
parser.require(XmlPullParser.END_TAG, null, "text");
}
parser.require(XmlPullParser.END_TAG, null, "elements");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}