java使用rome.jar实现rss阅读功能

rome是一个很好用的解析rss的工具包,支持rote格式,最近在项目中使用了此包,共享给大家。在此只公开部分代码public class FeedReader {

	public static void main(String[] args) {
		String source = "http://www.36kr.com/feed";
		// source = "http://news.163.com/special/00011K6L/rss_newstop.xml";
		// source = "http://hi.baidu.com/ybhanxiao/rss";
		// source = "http://ybhanxiao.iteye.com/rss";
		// source="http://www.luoyundeng.com/?feed=rss2";
		// source = "http://yanglanvip.blog.sohu.com/rss";
		// source="http://feed.williamlong.info/";
		try {

			SyndFeed feed = readFeed(source);
			ReaderSource readerSource = new ReaderSource();
			if (feed != null) {

				getArticles(feed, readerSource, null);
			}
			System.out.println(readerSource.getSourceName() + readerSource.getSourceDesc());

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (FeedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			System.out.print("异常" + e);
		}
	}

	/**
	 * 请求rss并返回syndFeed
	 * 
	 * @param source
	 * @return
	 * @throws IllegalArgumentException
	 * @throws FeedException
	 * @throws IOException
	 * @author echo
	 * @date 2012-1-11
	 */
	@SuppressWarnings("unchecked")
	public static SyndFeed readFeed(String source) throws IllegalArgumentException, FeedException, IOException {

		// SyndFeedInput:从远程读到xml结构的内容转成SyndFeedImpl实例
		SyndFeedInput input = new SyndFeedInput();
		// Locale.setDefault(Locale.ENGLISH);
		SyndFeed feed = null;
		if (source.startsWith("http")) {
			URLConnection feedUrl = new URL(source).openConnection();
			/**
			 *解决此异常: java.io.IOException: Server returned HTTP response code:
			 * 403 for URL 但是自己却可以用浏览器访问,发现可能是服务器对java程序屏蔽了。
			 * 因为服务器的安全设置不接受Java程序作为客户端访问,解决方案是设置客户端的User Agent
			 */
			feedUrl.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
			feedUrl.setConnectTimeout(5000);

			feed = input.build(new XmlReader(feedUrl));

		} else {
			File feedUrl = new File(source);
			feed = input.build(new XmlReader(feedUrl));

		}
		return feed;
	}}

如果解析某些站点时候日期出现为空时可以再src下加入rome.properties文件,并将datetime.extra.masks的指改为如下:

 

yyyy-MM-dd HH:mm:ss|yyyy-MM-dd HH:mm|yyyy-MM-dd  

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值