---
title: "开发RSS聚合的开源框架"
author: "benbenming"
date: "2009-03-11T20:01:19Z"
tags: []
slug: "rss"
draft: false
lastmod: "2009-03-11T20:01:19Z"
id: 347620
url: "https://benbenming.iteye.com/blog/347620"
---

最近公司要开发一个基与java的RSS阅读器，但是时间紧，在网上找个了一个开源的框架Rome,Rome并不是指罗马。不要误会，这里介绍的rome为一个开源java框架，可以在[www.java.net](http://www.java.net/)上找到它，它是为RSS聚合而开发的一个框架，让你可以快速的开发基于java的RSS阅读，发布器，支持

- RSS 0.91 Netscape
- RSS 0.91 Userland
- RSS 0.92
- RSS 0.93
- RSS 0.94
- RSS 1.0
- RSS 2.0
- Atom 0.3
- Atom 1.0

  等标准。你也可以自己扩展他的Module，让他解析你自己定义的XML格式。可以说它是一种用途很广的框架，在使用它之前。你必须下载JDOM开发包，因为它是用JDOM解析XML的。

在Rome中主要包裹下面几个包

com.sun.syndication.feed

com.sun.syndication.feed.atom

与获得atom中各个节点的数据

com.sun.syndication.feed.module

modeule为各个数据模型层。可以自己扩展

com.sun.syndication.feed.rss

用与获得RSS中各个相对应的XML节点中的值

com.sun.syndication.feed.sse  
com.sun.syndication.feed.synd

此包为写成RSS格式的XML设置个节点的属性  
com.sun.syndication.io

此包为输入输出流。

它的UML图

![](http://p.blog.csdn.net/images/p_blog_csdn_net/fishkey/HowRomeWorks.png)

 

                                   图1

下面是一个例子，功能是生成RSS格式的XML

public class FeedWriter {

    private static final String DATE\_FORMAT = "yyyy-MM-dd";

    public static void main(String\[] args) {  
        boolean ok = false;  
        if (args.length==2) {  
            try {  
                String feedType = args\[0];  
                String fileName = args\[1];

                DateFormat dateParser = new SimpleDateFormat(DATE\_FORMAT);

                SyndFeed feed = new SyndFeedImpl();  //feed流  
                feed.setFeedType(feedType);                   //设置rss版本

                feed.setTitle("Sample Feed (created with Rome)");  //&lt;title&gt;设置标题  
                feed.setLink([http://www.csdn.net](http://www.csdn.net/));                               //&lt;link&gt;  
                feed.setDescription("This feed has been created using Rome (Java syndication utilities");

                List entries = new ArrayList();  
                SyndEntry entry;  
                SyndContent description;

                entry = new SyndEntryImpl();                                 //子节点  
                entry.setTitle("Rome v1.0");  
                entry.setLink([http://www.csdn.net](http://www.csdn.net/));  
                entry.setPublishedDate(dateParser.parse("2004-06-08"));  
                description = new SyndContentImpl();  
                description.setType("text/plain");  
                description.setValue("Initial release of Rome");  
                entry.setDescription(description);  
                entries.add(entry);

                entry = new SyndEntryImpl();  
                entry.setTitle("Rome v2.0");  
                entry.setLink([http://ww.csdn.net](http://ww.csdn.net/));  
                entry.setPublishedDate(dateParser.parse("2004-06-16"));  
                description = new SyndContentImpl();            //描述  
                description.setType("text/xml");  
                description.setValue("Bug fixes, &lt;xml&gt;XML&lt;/xml&gt; minor API changes and some new features");  
                entry.setDescription(description);  
                entries.add(entry);

                entry = new SyndEntryImpl();  
                entry.setTitle("Rome v3.0");  
                entry.setLink("http://www.csdn.net");  
                entry.setPublishedDate(dateParser.parse("2004-07-27"));  
                description = new SyndContentImpl();  
                description.setType("text/html");  
                description.setValue("&lt;p&gt;More Bug fixes, mor API changes, some new features and some Unit testing&lt;/p&gt;"+  
                                     "&lt;p&gt;For details check the &lt;a href=\\"http://www.csdn.net\\"&gt;Changes Log&lt;/a&gt;&lt;/p&gt;");  
                entry.setDescription(description);  
                entries.add(entry);

                feed.setEntries(entries);                             //设置子节点

                Writer writer = new FileWriter(fileName);  
                SyndFeedOutput output = new SyndFeedOutput();  
                output.output(feed,writer);                        //写到文件中去  
                
                writer.close();

                System.out.println("The feed has been written to the file \["+fileName+"]");

                ok = true;  
            }  
            catch (Exception ex) {  
                ex.printStackTrace();  
                System.out.println("ERROR: "+ex.getMessage());  
            }  
        }

        if (!ok) {  
            System.out.println();  
            System.out.println("FeedWriter creates a RSS/Atom feed and writes it to a file.");  
            System.out.println("The first parameter must be the syndication format for the feed");  
            System.out.println("  (rss\_0.90, rss\_0.91, rss\_0.92, rss\_0.93, rss\_0.94, rss\_1.0 rss\_2.0 or atom\_0.3)");  
            System.out.println("The second parameter must be the file name for the feed");  
            System.out.println();  
        }  
    }

}  
     就这么多了。第一次写blog写的不好。大家不要砸砖头：）