kxml 解析xml 姓名 内容显示

package blogkxml;


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 url="http://localhost:8080/abc"; //servlet
			try {
				HttpConnection hc=(HttpConnection)Connector.open(url);
				KXmlParser parser=new KXmlParser();
				parser.setInput(hc.openInputStream(), "UTF-8");
				  parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
			        int eventType=parser.getEventType();
			        while(eventType!=XmlPullParser.END_DOCUMENT){
			        if(eventType==XmlPullParser.START_TAG){
			        	String tag=parser.getName();
			        	eventType=parser.next();
			        	if("Blogs".equals(tag)){

			        	}else if("Blog".equals(tag)){

			        	}else if("Content".equals(tag)){
			        		System.out.println(parser.getText());
			        		String content=parser.getText();
			        		if(content!=null){
			        			frm.append(parser.getText());
			        		}
			        	}else if("Name".equals(tag)){
			        		System.out.println(parser.getText());
			        		frm.append(parser.getText()+"说:");
			        		
			        	}
			        }else if(eventType==XmlPullParser.END_TAG){

			        }
			        	eventType=parser.next();
			        }
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}

}

 

 

你可能感兴趣的:(C++,c,xml,servlet,C#)