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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值