首页
http://wiki.java.net/bin/view/Javawsxml/Rome05TutorialFeedServlet
文件
rome-1.0RC1.jar
1、配置RSS服务
其实挺简单,没啥好配置的,我是配置在我的spring mvc controller中使用的
先在pom.xml中导入这个东东,先导入到maven2私服,然后修改pom.xml
<dependency>
<groupId>rome</groupId>
<artifactId>rome</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
嘿嘿,修改springside上的BaseManageController基类(我用的springside2上的基类,感觉还挺清晰的)
增加了如下方法
/**
* RSS支持
* @param request
* @param response
* @return
* @throws Exception
*/
public ModelAndView rss(HttpServletRequest request,
HttpServletResponse response) throws Exception {
onRss(request, response);
return null;
}
//等着被overwrite
protected void onRss(HttpServletRequest request,
HttpServletResponse response) throws Exception {
}
在基类BaseController中加入如下代码:
private static final String MIME_TYPE = "application/xml; charset=UTF-8";
// rss_0.90, rss_0.91, rss_0.92, rss_0.93, rss_0.94, rss_1.0, rss_2.0,
// atom_0.3
private static final String RSS_TYPE = "rss_2.0";
//我直接写死的rss_2.0,其实也没有啥好配置的
/**
* 直接向客户端返回,不用通过View页面渲染.
*/
protected void rendText(HttpServletResponse response, SyndFeed feed)
throws IOException {
response.setCharacterEncoding("UTF-8");
feed.setFeedType(RSS_TYPE);
response.setContentType(MIME_TYPE);
SyndFeedOutput output = new SyndFeedOutput();
try {
output.output(feed, response.getWriter());
} catch (IOException e) {
e.printStackTrace();
} catch (FeedException e) {
e.printStackTrace();
}
}
找了个UserContoller.java来演示一下,当然实际中,应该是从manager层得到数据吧,这里我是MOCK一些数据来DEMO的
protected void onRss(HttpServletRequest request,
HttpServletResponse response) throws Exception {
SyndFeed feed = new SyndFeedImpl();
feed.setTitle("Channel title");
feed.setLink("http://hi.baidu.com/luohuazju");
feed.setDescription("Channel title, java demo");
List entries = new ArrayList();
SyndEntry entry;
SyndContent description;
entry = new SyndEntryImpl();
entry.setTitle("RSS demo");
entry.setLink("http://hi.baidu.com/luohuazju");
entry.setPublishedDate(new Date());
description = new SyndContentImpl();
description.setType("text/plain");
description.setValue("just the guide in the rome site");
entry.setDescription(description);
entries.add(entry);
feed.setEntries(entries);
rendText(response, feed);
}
服务配置就结束了,弄个reader来读取下看。
2、reader读取RSS
核心代码如下:
URL feedUrl = null;
try {
feedUrl = new URL("http://www.kiko.com/search/user.do?method=rss");
} catch (MalformedURLException e) {
e.printStackTrace();
}
SyndFeedInput input = new SyndFeedInput();
try {
SyndFeed feed = input.build(new XmlReader(feedUrl));
System.out.println(feed.getTitle());
List list = feed.getEntries();
for (int i = 0; i < list.size(); i++) {
SyndEntry entry = (SyndEntry) list.get(i);
System.out.println(entry.getTitle() + " : "
+ entry.getPublishedDate());
System.out.println(entry.getLink());
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (FeedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
就上面的server和reader的列子,读出的结果是:
Channel title