xml文件解析

package http;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;


/**
* 多选列表框的处理
* The MIDlet demonstrates the different types of list supported by MIDP-2.0:
*      EXCLUSIVE - a choice having exactly one element selected at time.
*      IMPLICIT  - a choice in which the currently focused element is
*                  selected when a Command is initiated.
*      MULTIPLE  - a choice that can have arbitrary number of elements
*                  selected at a time.
*
* @version 2.0
*/


public class ReadRss
    extends MIDlet
    implements CommandListener {
    private final static Command exitCommand =
     new Command("Exit", Command.EXIT, 1);
    private final static Command backCommand =
     new Command("Back", Command.BACK, 1);
    private final static Command okCommand =
     new Command("Ok",Command.OK,1);
    private Display display;
    private List mainList;
    private TextBox tb;
   
   
    public ReadRss() {
        display = Display.getDisplay(this);
        tb = new TextBox("List Example","List",100000,0);
        tb.addCommand(exitCommand);
        tb.addCommand(backCommand);
        tb.setCommandListener(this);
        // these are the strings for the choices.
        
    }

    protected void startApp() {
      
   
            // these are the images and strings for the choices.
     Vector titleVector=null;   
     try{
            titleVector=getTitleArray();
            }catch(Exception ex){
             ex.printStackTrace();
            }
            if(titleVector!=null){
             int size=titleVector.size();
             System.out.println(size);
             String[] stringArray = new String[size];
             for(int i=0;i<size;i++){
              stringArray[i]=titleVector.elementAt(i).toString();
             }
             mainList = new List("Choose textbox", Choice.IMPLICIT, stringArray,
                                 null);
            
             mainList.addCommand(exitCommand);
             mainList.addCommand(okCommand);
             mainList.setCommandListener(this);
             display.setCurrent(mainList);
            }
           
        }
   

    protected void destroyApp(boolean unconditional) {
    }

    protected void pauseApp() {
    }

    public void commandAction(Command c, Displayable d) {   
        if (d.equals(mainList)) {
            // in the main list
            if (c == okCommand) {
                if (d.equals(mainList)) {
                 String title=mainList.getString(mainList.getSelectedIndex());
                 
                 tb.setTitle(title);
                 tb.setString(getDetail(title));
           display.setCurrent(tb);
                  
                }
            }
        } else {
            // in one of the sub-lists
            if (c == backCommand) {
                display.setCurrent(mainList);
            }
        }

        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
   
    public Vector getTitleArray() {
     Vector titleArray=new Vector();
     String url = "http://www.xrss.cn/WebRss.Asp";
  HttpConnection c = null;
  InputStream is = null;
  InputStreamReader in=null;
  KXmlParser parser=null;
  int eventType=0;
  try{
  c = (HttpConnection) Connector.open(url);
  in = new InputStreamReader(c.openDataInputStream());
  parser = new KXmlParser();
  parser.setInput(in);
  eventType = parser.getEventType();
  String title;
  while (eventType != XmlPullParser.END_DOCUMENT) {
   if (eventType == XmlPullParser.START_TAG
     && parser.getName().equals("title")) {
    eventType = parser.next();
    title=parser.getText();
    if(title.length()>30)
     title=title.substring(0,30);
    titleArray.addElement(title);
   }

   eventType =  parser.next();
  }
  }
  catch(Exception ex){
   ex.printStackTrace();
  }
  finally{
   
    try {
     if(in!=null)
      
     in.close();
     if(is!=null)
      
      is.close();
     if(c!=null)
      c.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  titleArray.removeElementAt(0);
  titleArray.removeElementAt(1);
     return titleArray;
    }
   
    public String  getDetail(String title) {
     String detail=null;
     String url = "http://www.xrss.cn/WebRss.Asp";
  HttpConnection c = null;
  InputStream is = null;
  InputStreamReader in=null;
  KXmlParser parser=null;
  int eventType=0;
  try{
  c = (HttpConnection) Connector.open(url);
  in = new InputStreamReader(c.openDataInputStream());
  parser = new KXmlParser();
  parser.setInput(in);
  eventType = parser.getEventType();
  while (eventType != XmlPullParser.END_DOCUMENT) {
  if (eventType == XmlPullParser.START_TAG
     && parser.getName().equals("title")) {
    if(parser.nextText().indexOf(title)!=-1){
     parser.nextTag();
     parser.nextText();
     parser.nextTag();
     detail=parser.nextText().toString();
     return detail;
    }
   
  }

   eventType =  parser.next();
  }
  }
  catch(Exception ex){
   ex.printStackTrace();
   
  }
  return null;
    }
}

你可能感兴趣的:(xml,exception,vector,String,command,textbox)