kxml 解析xml 高级

package http;


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/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_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");
			        		}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();
			        }
				
				

			} catch (Exception e) {
				e.printStackTrace();
	
			}
		}
		
	}

}



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 HttpServletXmlRequire 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 HttpServletXmlRequire() {
	}

	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>"  
					   +"<abc>text2</abc>"  
					   +"</elements>";  

			try {
				ByteArrayInputStream bin = new ByteArrayInputStream( xml.getBytes() );
		        KXmlParser parser = new KXmlParser();		       		        
		        parser.setInput(new InputStreamReader(bin));	    
		      
		         parser.nextTag();   
//		        parser.require(XmlPullParser.START_TAG, null, "elements");   
		         
		        while(parser.nextTag() == XmlPullParser.START_TAG) {   
//			           parser.require(XmlPullParser.START_TAG, null, "text");    //nexTag 和 这里要匹配 否则报错
			           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();
	
			}
		}
		
	}

}
 

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