用Java+JDOM寫的RSS閱讀軟體

來源:互聯網
上載者:User
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個:

  1. title :channel的名字,上例中為 Liftoff News
  2. link :channel的url地址,上例中為 http://liftoff.msfc.nasa.gov/
  3. description :對channel的描述,上例中為 Liftoff to Space Exploration.
  4. item :這個標籤有很多個,表明channel中的不同條目,比如不同的新聞

除了以上的標籤,channel還可以包含很多標籤。具體的可以參考英文文檔。其中item標籤又包含很多子標籤,如下所示:

  1. title :item的名字
  2. link :item的url地址
  3. description :對item的描述
  4. author :編寫這一item的作者的郵件地址
  5. guid :標識這一item的一個唯一的字串
  6. ..............

更多的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;
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.