android 中如何解析Rss订阅的xml文件

上代码:

package com.android.rssreader;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.os.Bundle;

public class RssReaderActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
   
    private void ParseRss(int thd, InputStream in) throws IOException, XmlPullParserException {
     String title = "";
     String pubDate = "";
     String link = "";
     XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
     factory.setNamespaceAware(true);
     XmlPullParser xpp = factory.newPullParser();
     xpp.setInput(in, null); //null 为编码格式,如utf-8,null为所有的。
     int eventType = xpp.getEventType();
     String tag = xpp.getName();
     
     //解析文件的头
     while(eventType != XmlPullParser.END_DOCUMENT) {
      if(eventType == XmlPullParser.START_TAG) {
       if(tag.equals("item")) {
        return;
       } else if(tag.equals("title")) {
        xpp.next();
        title = xpp.getText().toString();
       } else if(tag.equals("pubDate")) {
        xpp.next();
        pubDate = xpp.getText().toString();
       } else if(tag.equals("link")) {
        xpp.next();
        link = xpp.getText().toString();
       }
      } else if(eventType == XmlPullParser.END_TAG) {
       if(tag.equals("link")) {
        RssHead head = new RssHead(title, pubDate, link);
       }
      }
      eventType = xpp.next();
     }
     
     
     //解析文件的Item项
     String item_title = null;
     String item_pubtime = null;
     String item_link = null;
     String description = null;
     while(eventType != XmlPullParser.END_DOCUMENT) {
      if(eventType == XmlPullParser.START_TAG) {
       if(tag.equals("item")) {
        xpp.next();
        item_title = item_pubtime = item_link = description = "";
       } else if(tag.equals("title")) {
        xpp.next();
        item_title = xpp.getText().toString();
       } else if(tag.equals("link")) {
        xpp.next();
        item_link = xpp.getText().toString();
       } else if(tag.equals("pubTime")) {
        xpp.next();
        item_pubtime = xpp.getText().toString();
       } else if(tag.equals("description")) {
        xpp.next();
        description = xpp.getText().toString();
       }
      } else if(eventType == XmlPullParser.END_TAG) {
       if(tag.equals("item")) {
        RssItem ri = new RssItem(item_title,item_pubtime,item_link,description);
       }
      }
     }
     
     
     //以上的解析方法是针对双标签,例如<title>title</title>
     //下面的解析方法是针对的单标签,例如:<title href="www.google.cn"/>
     //这里我只写出大体的过程,具体自己琢磨了。
     
     while(eventType != XmlPullParser.END_DOCUMENT) {
      if(eventType == XmlPullParser.START_TAG) {
       if(tag.equals("title")) {
        //注意,这里不能用next()了,循环完了在用。
        for(int i=0; i<xpp.getAttributeCount(); i++) {
         if(xpp.getAttributeName(i).equals("href")) {
          String link2 = xpp.getAttributeValue(i);
          break;
         }
        }
        xpp.next();
       }
      }
     }
     
    }
}

 

 

下面的是RssItem.java和RssHead.java, 写的很粗糙。

 

package com.android.rssreader;

public class RssHead {

 private String title;
 private String pubDate;
 private String link;
 
 public RssHead(String title, String pubDate, String link) {
  // TODO Auto-generated constructor stub
  this.link = link;
  this.pubDate = pubDate;
  this.title = title;
 }
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public String getPubDate() {
  return pubDate;
 }
 public void setPubDate(String pubDate) {
  this.pubDate = pubDate;
 }
 public String getLink() {
  return link;
 }
 public void setLink(String link) {
  this.link = link;
 }
}

 

 

 

package com.android.rssreader;

public class RssItem {

 private String item_title;
 private String item_pubDate;
 private String item_link;
 private String description;
 
 public RssItem(String title, String putDate, String link, String description) {
  // TODO Auto-generated constructor stub
  item_title = title;
  item_pubDate = putDate;
  item_link = link;
  this.description = description;
 }
 public String getItem_title() {
  return item_title;
 }
 public void setItem_title(String item_title) {
  this.item_title = item_title;
 }
 public String getItem_pubDate() {
  return item_pubDate;
 }
 public void setItem_pubDate(String item_pubDate) {
  this.item_pubDate = item_pubDate;
 }
 public String getItem_link() {
  return item_link;
 }
 public void setItem_link(String item_link) {
  this.item_link = item_link;
 }
 public String getDescription() {
  return description;
 }
 public void setDescription(String description) {
  this.description = description;
 }
}

 

 

还有,InputString 应该从Connection中拿到,还应该写一个获取连接的方法,今天太晚了,明天继续吧。

你可能感兴趣的:(android)