Java實現RSS

來源:互聯網
上載者:User
RSS是一個標準的XML檔案,Rss閱讀器可以讀取這個XML檔案獲得文章的資訊,使使用者可以通過Rss閱讀器而非瀏覽器閱讀Blog,我們只要動態產生這個XML檔案便可以了。RSSLibJ是一個專門讀取和產生RSS的小巧實用的Java庫,大小僅25k,可以從http://sourceforge.net/projects/rsslibj/下載rsslibj-1_0RC2.jar和它需要的EXMLjar兩個檔案,然後複製到web/WEB-INF/lib/下。  rsslibj-1_0RC2.jar:http://sourceforge.net/project/downloading.php?group_id=71153&use_mirror=nchc&filename=rsslibj-1_0RC2.jar&27763931  EXML.jar:http://rsslibj.cvs.sourceforge.net/rsslibj/rsslibj/lib/EXML.jar?view=log  下載下來之後把 擴張名.zip 改為 .jar 就行了。
import com.rsslibj.elements.Channel;public class Writer {    public static void main(String[] args)             throws InstantiationException, ClassNotFoundException,             IllegalAccessException {        Channel channel=new Channel();        channel.setDescription("This is my sample channel.");        channel.setLink("http://localhost/");        channel.setTitle("My Channel");        channel.setImage("http://localhost/",                 "The Channel Image",                 "http://localhost/foo.jpg");        channel.setTextInput("http://localhost/search",                 "Search The Channel Image",                 "The Channel Image",                 "s");        channel.addItem("http://localhost/item1",                "The First Item covers details on the first item>",                "The First Item")                .setDcContributor("Joseph B. Ottinger");        channel.addItem("http://localhost/item2",                "The Second Item covers details on the second item",                "The Second Item")                .setDcCreator("Jason Bell");        System.out.println("The feed in RDF: "+channel.getFeed("rss"));    }}
Channel channel = new Channel(); channel.setDescription(account.getDescription()); baseUrl = baseUrl.substring(0, n); channel.setLink("http://server-name/home.c?accountId=" + accountId); channel.setTitle(account.getTitle()); List articles = facade.getArticles(accountId, account.getMaxPerPage(), 1); Iterator it = articles.iterator(); while(it.hasNext()) {     Article article = (Article)it.next();     channel.addItem("http://server-name/article.c?articleId=" + article.getArticleId(),         article.getSummary(), article.getTitle()     ); } // 輸出xml: response.setContentType("text/xml"); PrintWriter pw = response.getWriter(); pw.print(channel.getFeed("rss")); pw.close(); 

 

本文轉自 “霜之哀傷” 部落格,出處http://maomao.blog.51cto.com/115985/22180

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.