最近公司要开发一个基与java的RSS阅读器,但是时间紧,在网上找个了一个开源的框架Rome,Rome并不是指罗马。不要误会,这里介绍的rome为一个开源java框架,可以在www.java.net上找到它,它是为RSS聚合而开发的一个框架,让你可以快速的开发基于java的RSS阅读,发布器,支持
等标准。你也可以自己扩展他的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图
图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)"); //<title>设置标题
feed.setLink(http://www.csdn.net); //<link>
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);
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);
entry.setPublishedDate(dateParser.parse("2004-06-16"));
description = new SyndContentImpl(); //描述
description.setType("text/xml");
description.setValue("Bug fixes, <xml>XML</xml> 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("<p>More Bug fixes, mor API changes, some new features and some Unit testing</p>"+
"<p>For details check the <a href=\"http://www.csdn.net\">Changes Log</a></p>");
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写的不好。大家不要砸砖头:)