RSS的读取和发布

这几天研究了RSS

RSS(简易信息聚合,也叫聚合内容)是一种描述和同步网站内容的格式。RSS可以是以下三个解释的其中一个: Really Simple Syndication;RDF (Resource Description Framework) Site Summary; Rich Site Summary。但其实这三个解释都是指同一种Syndication的技术。RSS目前广泛用于网上新闻频道,blog和wiki,主要的版本有0.91, 1.0, 2.0。使用RSS订阅能更快地获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新。网络用户可以在客户端借助于支持RSS的聚合工具软件,在不打开网站内容页面的情况下阅读支持RSS输出的网站内容。


要研究的问题
1、如何读取RSS,比如sina提供了一个RSS,我如何读取这些信息
2、我自己有一个WEB应用,如何发布RSS
3、使用哪些工具可以聚合RSS


第一个问题,我们可以使用开源的jar包 rome-1.0.jar 、jdom.jar
参考地址:
直接上代码吧


import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Properties;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
*
* 查询rss
*
* @author bdk197431
* @date 2013-1-21 上午11:40:25
*
* @version 1.0
*/
public class Test1
{
public static void main(String[] args) throws Exception
{


// 设置代理服务器
Properties systemSettings = System.getProperties();

systemSettings.put("http.proxyHost", "101.11.11.1");

systemSettings.put("http.proxyPort", "80");

System.setProperties(systemSettings);

//

URL feedUrl = new URL("http://rss.sina.com.cn/news/marquee/ddt.xml");

SyndFeedInput input = new SyndFeedInput();

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

//
URLConnection feedUrlConn = new URL("http://rss.sina.com.cn/news/marquee/ddt.xml").openConnection();

feedUrlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

SyndFeedInput input2 = new SyndFeedInput();

SyndFeed feed2 = input2.build(new XmlReader(feedUrl));

// 查询所有 rss
List list = feed2.getEntries();

for (int i = 0; i < list.size(); i++)
{

SyndEntry entry = (SyndEntry) list.get(i);
System.out.println(entry);
System.out.println("------------------");
}
}
}




第二个问题:

可以用java代码生成xml文件,这样不知道是否可以发布RSS,


import java.io.FileWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;

public class RssWrite
{

public static void main(String[] args) throws Exception
{
DateFormat dateParser = new SimpleDateFormat("yyyyMMddhhmmss");

SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("Sample Feed (created with Rome)");
feed.setLink("http://rome.dev.java.net");
feed.setDescription("This feed has been created using Rome (Java syndication utilities");

List<SyndEntry> entries = new ArrayList<SyndEntry>();
SyndEntry entry;
SyndContent description;

// item 内容
// 重复add产生多个item
entry = new SyndEntryImpl();
entry.setTitle("Rome v1.0");
entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
entry.setPublishedDate(new Date());
description = new SyndContentImpl();
description.setType("text/plain");
description.setValue("hello caohaigang");
entry.setDescription(description);
entries.add(entry);

feed.setEntries(entries);

String fileName = "c:\\rss\\feed" + dateParser.format(new Date()) + ".xml";
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 + "]");
}
}





<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>Sample Feed (created with Rome)</title>
<link>http://rome.dev.java.net</link>
<description>This feed has been created using Rome (Java syndication utilities</description>
<item>
<title>Rome v1.0</title>
<link>http://wiki.java.net/bin/view/Javawsxml/Rome01</link>
<description>hello caohaigang</description>
<pubDate>Mon, 21 Jan 2013 11:52:52 GMT</pubDate>
<guid>http://wiki.java.net/bin/view/Javawsxml/Rome01</guid>
<dc:date>2013-01-21T11:52:52Z</dc:date>
</item>
</channel>
</rss>




补充一点: 生成出来的XML,在IE8下面可以正常显示RSS ,不过到Goole chmod下显示失败 ,在网上搜索资料后 在xml的第二行增加
<?xml-stylesheet type="text/xsl" href="rss.xslt"?>
[img]http://dl.iteye.com/upload/attachment/0079/6460/1b8f0bce-29d0-31b1-ab53-2c11d75975ea.png[/img]
[img]http://dl.iteye.com/upload/attachment/0079/6462/ddf37628-422e-33a4-839f-b3aee2132c84.png[/img]


第三个问题
有人说用抓虾,不过这是很早以前的事情,听说最近都快关门了
FeedDemon 、Google Reader ,可以使用下看看
<think>我们正在讨论如何配置读取RSSfeed。根据用户的问题,他需要知道配置读取RSSfeed的方法或工具。我们可以从以下几个方面来回答:1.什么是RSS feed?2.如何配置RSS feed(生成RSS feed)?3.如何读取RSS feed(解析RSSfeed)? 同时,根据用户提供的两个引用,我们可以知道: -引用[1]展示了使用PHP读取RSS feed的例子,使用了`file_get_contents`函数获取RSS内容。-引用[2]展示了使用Java读取RSSfeed的例子,使用了`SyndFeed`类来解析。 因此,我们可以提供多种语言的实现方法,如PHP、Java,也可以包括其他常见语言如Python。另外,用户可能还需要知道一些现成的工具来配置读取RSS feed,比如在线RSS阅读器、RSS生成工具等。 下面我们组织回答:</think>### 如何配置读取RSS Feed RSS(Really Simple Syndication)是一种用于发布网站更新的标准格式。以下是配置读取RSS Feed的完整方法: #### **一、配置RSS Feed(生成RSS源)** 1. **基础XML结构** RSS本质是XML文件,需遵循特定格式。基本结构如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"> <channel> <title>网站标题</title> <link>https://example.com</link> <description>网站描述</description> <!-- 条目示例 --> <item> <title>文章标题</title> <link>https://example.com/article</link> <description>文章摘要</description> <pubDate>Wed, 15 May 2024 12:00:00 GMT</pubDate> </item> </channel> </rss> ``` 2. **动态生成工具** - **内容管理系统(CMS)**:如WordPress内置RSS生成功能,访问 `https://your-site.com/feed` 即可获取 - **编程生成**(PHP示例): ```php header("Content-type: application/rss+xml; charset=utf-8"); echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<rss version="2.0"><channel>'; echo '<title>我的网站</title>'; // 从数据库循环获取内容 while ($item = fetch_from_db()) { echo '<item>'; echo '<title>' . htmlspecialchars($item['title']) . '</title>'; echo '<link>' . $item['url'] . '</link>'; echo '</item>'; } echo '</channel></rss>'; ``` #### **二、读取RSS Feed(解析订阅源)** 1. **使用现成阅读器** - 在线工具:Feedly、Inoreader - 本地应用:Thunderbird(支持Windows/Linux/Mac) 2. **编程解析方法** - **PHP解析**(引用[1]): ```php $rss = simplexml_load_file('http://example.com/rss.xml'); foreach ($rss->channel->item as $item) { echo "标题: " . $item->title . "<br>"; echo "链接: " . $item->link . "<hr>"; } ``` - **Java解析**(引用[2]): ```java import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.io.SyndFeedInput; import java.net.URL; public class RSSReader { public static void main(String[] args) throws Exception { URL feedUrl = new URL("http://example.com/rss.xml"); SyndFeed feed = new SyndFeedInput().build(new XmlReader(feedUrl)); feed.getEntries().forEach(entry -> { System.out.println("标题: " + entry.getTitle()); System.out.println("链接: " + entry.getLink()); }); } } ``` 需添加依赖:`rome`(用于RSS解析) - **Python解析**: ```python import feedparser feed = feedparser.parse("http://example.com/rss.xml") for entry in feed.entries: print(f"标题: {entry.title}") print(f"链接: {entry.link}") ``` #### **三、高级配置技巧** 1. **验证RSS有效性** 使用W3C验证工具:https://validator.w3.org/feed/ 2. **支持内容更新** 添加 `<lastBuildDate>` 标签到 `<channel>` 中,例如: ```xml <lastBuildDate>Wed, 15 May 2024 09:30:00 GMT</lastBuildDate> ``` 3. **多媒体支持** 嵌入图片/视频: ```xml <item> <enclosure url="https://example.com/image.jpg" type="image/jpeg" /> </item> ``` > **提示**:实际开发中需注意时区处理(建议使用GMT时间)内容编码(推荐UTF-8),避免解析错误[^1][^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值