RSS閱讀軟體。
RSS是Really Simple Syndication的縮寫,是一種基於XML的檔案交換標準,一般用於新聞標題的快速閱讀。其實,RSS源就是一個XML檔案,所以這個RSS閱讀器主要的部分就在於解析XML文檔。我用了JDOM解析XML文檔,感覺JDOM還是比較簡單的。
首先介紹一下RSS標準,他的英文文檔可見這裡。
一下是一個RSS檔案的例子:
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>Liftoff News</title>
<link>http://liftoff.msfc.nasa.gov/</link>
<description>Liftoff to Space Exploration.</description>
<language>en-us</language>
<item>
<title>Star City</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
<description>American Life</description>
<pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
</item>
<item>
<description>Europe Life</description>
<pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
</item>
</channel>
</rss>
RSS檔案是一個XML檔案,根項目是<rss>標籤,它標明了RSS檔案的版本,上面的RSS檔案是2.0版本。我寫的這個閱讀器也只考慮了2.0版本。
在<rss>標籤下是<channel>標籤,它說明的這個RSS源的主要屬性。<channel>標籤可以包含和多子標籤,其中必須的是4個:
- title :channel的名字,上例中為 Liftoff News
- link :channel的url地址,上例中為 http://liftoff.msfc.nasa.gov/
- description :對channel的描述,上例中為 Liftoff to Space Exploration.
- item :這個標籤有很多個,表明channel中的不同條目,比如不同的新聞
除了以上的標籤,channel還可以包含很多標籤。具體的可以參考英文文檔。其中item標籤又包含很多子標籤,如下所示:
- title :item的名字
- link :item的url地址
- description :對item的描述
- author :編寫這一item的作者的郵件地址
- guid :標識這一item的一個唯一的字串
- ..............
更多的item的標籤可以參考英文文檔。
系統結構
由於是學控制的,所以我習慣於把我的應用程式稱為一個系統。用物件導向的方法,系統的類圖如下所示:
上面的RSSContent類用於儲存從RSS源檔案解析出的資料,他們的定義可以從圖上清楚地看出。
RssXMLParser類用於從xmlfilename指定的RSS源解析資料,它運用JDOM解析XML檔案。
下面給出XML檔案解析的代碼:
static public RSSContent getRSSContent()
{
RSSContent rsscontent = new RSSContent();
try{
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(xmlfilename);
Element root = doc.getRootElement();
Element Channel = root.getChild("channel");
Element Child;
Child = Channel.getChild("title");
if(Child!=null)
rsscontent.channel.setTitle(Child.getContent(0).getValue());
Child = Channel.getChild("language");
if(Child!=null)
rsscontent.channel.setLanguage(Child.getContent(0).getValue());
Child = Channel.getChild("link");
if(Child!=null)
rsscontent.channel.setLink(Child.getContent(0).getValue());
Child = Channel.getChild("description");
if(Child!=null)
rsscontent.channel.setDescription(Child.getContent(0).getValue());
Child = Channel.getChild("pubDate");
if(Child!=null)
rsscontent.channel.setPubDate(Child.getContent(0).getValue());
Child = Channel.getChild("lastBuildDate");
if(Child!=null)
rsscontent.channel.setLastBuildDate(Child.getContent(0).getValue());
Child = Channel.getChild("docs");
if(Child!=null)
rsscontent.channel.setDocs(Child.getContent(0).getValue());
Child = Channel.getChild("generator");
if(Child!=null)
rsscontent.channel.setGenerator(Child.getContent(0).getValue());
Child = Channel.getChild("managingEditor");
if(Child!=null)
rsscontent.channel.setManagingEditor(Child.getContent(0).getValue());
Child = Channel.getChild("webMaster");
if(Child!=null)
rsscontent.channel.setWebMaster(Child.getContent(0).getValue());
Child = Channel.getChild("copyright");
if(Child!=null)
rsscontent.channel.setCopyright(Child.getContent(0).getValue());
List Items = Channel.getChildren("item");
Element Item;
for(int i=0;i<Items.size();i++){
RSSItem rssitem = new RSSItem();
Item = (Element)Items.get(i);
Child = Item.getChild("title");
if(Child!=null)
rssitem.title = Child.getContent(0).getValue();
Child = Item.getChild("link");
if(Child!=null)
rssitem.link = Child.getContent(0).getValue();
Child = Item.getChild("description");
if(Child!=null)
rssitem.description = Child.getContent(0).getValue();
rsscontent.channel.item.add(rssitem);
}
}catch(Exception e){
e.printStackTrace();
}
return rsscontent;
}