/**
* 根据链接地址得到数据
* @param url RSS形式的xml文件
* @throws IllegalArgumentException
* @throws FeedException
*/
public void parseXml(URL url) throws IllegalArgumentException, FeedException {
try {
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = null;
URLConnection conn;
conn = url.openConnection();
String content_encoding = conn.getHeaderField("Content-Encoding");
if (content_encoding != null && content_encoding.contains("gzip")) {
System.out.println("conent encoding is gzip");
GZIPInputStream gzin = new GZIPInputStream(conn
.getInputStream());
feed = input.build(new XmlReader(gzin));
} else {
feed = input.build(new XmlReader(conn.getInputStream()));
}
List entries = feed.getEntries();//得到所有的标题<title></title>
for(int i=0; i < entries.size(); i++) {
SyndEntry entry = (SyndEntry)entries.get(i);
System.out.println(entry.getTitle());
}
System.out.println("feed size:" + feed.getEntries().size());
} catch (IOException e) {
e.printStackTrace();
}
}
public void createXml() throws Exception {
/* 根据Channel源码提供的英文,Channel对象有两个构造器,一个默认的无参构造器用于clone对象,一个是有参的
* 我们自己指定的必须使用有参数的(因为我们需要许可证),指构造方法必须要创建一个type(版本),这个type不能随便写,必须要以rss_开头的版本号
* Licensed under the Apache License, Version 2.0 (the "License");
* 因为当前版本是2.0,所以就是rss_2.0,必须是rss_2.0否则会抛异常,该源码中写的已经很明白。
*/
Channel channel = new Channel("rss_2.0");
channel.setTitle("channel标题");//网站标题
channel.setDescription("channel的描述");//网站描述
channel.setLink("www.shlll.net");//网站主页链接
channel.setEncoding("utf-8");//RSS文件编码
channel.setLanguage("zh-cn");//RSS使用的语言
channel.setTtl(5);//time to live的简写,在刷新前当前RSS在缓存中可以保存多长时间(分钟)
channel.setCopyright("版权声明");//版权声明
channel.setPubDate(new Date());//RSS发布时间
List<Item> items = new ArrayList<Item>();//这个list对应rss中的item列表
Item item = new Item();//新建Item对象,对应rss中的<item></item>
item.setAuthor("hxliu");//对应<item>中的<author></author>
item.setTitle("新闻标题");//对应<item>中的<title></title>
item.setGuid(new Guid());//GUID=Globally Unique Identifier 为当前新闻指定一个全球唯一标示,这个不是必须的
item.setPubDate(new Date());//这个<item>对应的发布时间
item.setComments("注释");//代表<item>节点中的<comments></comments>
//新建一个Description,它是Item的描述部分
Description description = new Description();
description.setValue("新闻主题");//<description>中的内容
item.setDescription(description);//添加到item节点中
items.add(item);//代表一个段落<item></item>,
channel.setItems(items);
//用WireFeedOutput对象输出rss文本
WireFeedOutput out = new WireFeedOutput();
try {
System.out.println(out.outputString(channel));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (FeedException e) {
e.printStackTrace();
}
}
http://yueding920.blog.163.com/blog/static/352508902010521104722543/
分享到:
相关推荐
Rss 订阅 Demo Rss 订阅 Demo Rss 订阅 Demo Rss 订阅 Demo Rss 订阅 Demo
java实现rss的订阅
Rss订阅源码是构建Android手机RSS阅读器的源码。
asp.net rss订阅demo 框架3.5 通过RssToolkit组件 实现rss订阅 阅读
Rss订阅源码Demo.zip
Android源码——Rss订阅源码Demo.zip
Android Rss订阅源码Demo.rar
Android Rss订阅源码Demo.zip源码资源下载Android Rss订阅源码Demo.zip源码资源下载
基于JAVA SWING的RSS阅读器,使用JDOM解析本地XML文件
基于Java实现RSS的阅读器,有更新加载等功能。
Android源码——Rss订阅源码Demo.7z
基于Java的优秀RSS阅读器 有研究或探讨的请加群:37424970 或联系本人MSN或邮箱:zhuseahui@yahoo.com.cn
基于java的开发源码-XML写的RSS阅读器.zip 基于java的开发源码-XML写的RSS阅读器.zip 基于java的开发源码-XML写的RSS阅读器.zip 基于java的开发源码-XML写的RSS阅读器.zip 基于java的开发源码-XML写的RSS阅读器.zip ...
我当时整理的一个RSS的demo,支持RSS订阅。用JSP实现基于Web的RSS阅读器。
用JAVA写的RSS订阅器,使用了Spring MVC框架,可在tomcat上运行。
RSS在线订阅与阅读模块 RSS在线订阅与阅读模块 RSS在线订阅与阅读模块
安卓Android源码——Rss订阅源码Demo.zip