java生成rss组件

闲来无事写了一个java生成RSS的小组件,本人没有参考过其他的开源的组件,不过个人认为这种输出方式比较易用,
主要是用到了java的反射机制.

大概功能:
在你的PO里面定义某些annotaions 如:Author,Description,Guid,Link,Param,PubDate,Title
和你需要生成RSS里面的标签是同名的

当你从数据库取值返回List之后 值就已经进去了..

整个调用过程如下:


//这个list是从数据库取的
List placeList =placeService.getPlaceList(cecondition, pager, request);
//这个类是创建RSS模板以及生成XML格式的类
RssTemplet rssTemplet=RssTemplet.newInstance();
Channel channel;
try {
//生成具体的RSS模板
channel = rssTemplet.createRssTemplet(placeList);
//设置标题头信息
channel.setTitle("uuke 场所");
channel.setDescription("uuke 吃喝玩乐场所列表");
channel.setLink("http://www.uuke.cn");
channel.setGenerator("长沙最大的吃喝玩乐社区");
//具体的列表项
Set items=channel.getItems();
Iterator iterator=items.iterator();
while(iterator.hasNext()){
Item item=(Item)iterator.next();
item.setLink("http://www.uuke.cn/place/go_"+ item.getParams("placeId") +".html");
item.setDescription(
"<div id=\"box\">"+
"<img src=\"http://www.uuke.cn/"+ item.getParams("isimage") +"_2.gif\" />"+
"<p>地址:"+ item.getParams("placeAddr") +"</p>"+
"<p>电话:"+ item.getParams("homePhone") +"</p>"+
"<p>平均消费:"+ item.getParams("avgMonly") +"元/人</p>"+
"<p>"+ item.getDescription() +"</p>"+
"</div>"
);
item.setAuthor("uuke.cn");
item.setGuid(item.getLink());
}
sb=rssTemplet.createRss(channel);
} catch (Exception e) {
e.printStackTrace();
}


特别要说明的是item.getParams()
在RSS里面有一个link标签 作用是可以根据这个Link标签跳到具体的页面.
但这个时候需要得到对象的ID 或者其他的信息,才能跳过去.
所以你只需要在PO里面使用 params 的annotaion 来定义你的字段就可以获取到值

@Param
private Long id;
//gets sets
.....
item.getParams("id");就能取到


@Param("p_id")
private Long id;
//gets sets
item.getParams("p_id");
也能取到
.....

具体的请看源码..

查看效果 http://www.uuke.cn/place/findTypePlace---1---rss.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.cnfilm.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.cnfilm.web.film.Film; import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Description; import com.sun.syndication.feed.rss.Item; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.WireFeedOutput; /** * 文件名:RssUtils.java 网站RSS生成 * 版本信息:V1.0 * 日期:2013-06-18 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class RssUtils { public static String getRssString(List filmList,HashMap map){ Channel channel = new Channel("rss_2.0"); channel.setTitle(map.get("title")); channel.setDescription(map.get("description")); channel.setLink("http://www.bdvcd.com/"); channel.setEncoding("UTF-8"); /**这个list对应rss中的item列表**/ List items = new ArrayList(); /**新建Item对象,对应rss中的**/ Item item = null; for(Film film:filmList){ item = new Item(); item.setAuthor(film.getStr("starring")); item.setLink("http://www.bdvcd.com/"+film.getStr("curl")+"/"+film.getStr("url")+".html"); item.setPubDate(DateUtils.parse(film.getStr("addtime"))); item.setTitle(film.getStr("fname")); Description description = new Description(); description.setValue(film.getStr("content")); item.setDescription(description); items.add(item); } channel.setItems(items); /**用WireFeedOutput对象输出rss文本**/ WireFeedOutput out = new WireFeedOutput(); String rssString = ""; try { rssString = out.outputString(channel); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (FeedException e) { e.printStackTrace(); } return rssString; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值