1. Overview
1.概述
RSS (Rich Site Summary or Really Simple Syndication) is a web feed standard that provides readers with an aggregated content from various locations. The user can see what’s been published recently on his favorite blogs, news sites, etc – all in a single place.
RSS(Rich Site Summary或Really Simple Syndication)是一种网络提要标准,为读者提供来自不同地点的聚合内容。用户可以看到他喜欢的博客、新闻网站等最近发表的内容–所有这些都在一个地方。
Applications can also use RSS to read, manipulate, or publish information through RSS feeds.
应用程序也可以使用RSS来读取、操作或通过RSS提要发布信息。
This article gives an overview of how to process RSS feeds in Java with the Rome API.
本文概述了如何使用Rome API在Java中处理RSS提要。
2. Maven Dependencies
2.Maven的依赖性
We need to add the dependency for Rome API to our project:
我们需要将Rome API的依赖性添加到我们的项目中。
<dependency>
<groupId>rome</groupId>
<artifactId>rome</artifactId>
<version>1.0</version>
</dependency>
We can find the latest version on Maven Central.
我们可以在Maven Central上找到最新版本。
3. Creating a New RSS Feed
3.创建一个新的RSS提要
First, let’s create a new RSS feed with the Rome API using the default implementation SyndFeedImpl of the SyndFeed interface. This interface is able to handle all RSS flavors, so we can always feel safe to use it:
首先,让我们用Rome API创建一个新的RSS feed 使用SyndFeedImpl的默认实现SyndFeed接口。这个接口能够处理所有的RSS口味,所以我们可以一直放心地使用它。
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_1.0");
feed.setTitle("Test title");
feed.setLink("http://www.somelink.com");
feed.setDescription("Basic description");
In this snippet, we’ve created an RSS feed with standard RSS fields such as a title, link, and description. SyndFeed gives the opportunity of adding many more fields, including authors, contributors, copyrights, modules, published dates, images, foreign markups, and languages.
在这个片段中,我们创建了一个带有标准RSS字段(如标题、链接和描述)的RSS提要。SyndFeed提供了添加更多字段的机会,包括作者、贡献者、版权、模块、发布日期、图像、外国标记和语言。
4. Adding an Entry
4.增加一个条目
As we’ve created the RSS feed, now we can add an entry to it. In the example below, we use the default implementation SyndEntryImpl of the SyndEntry interface to create a new entry:
由于我们已经创建了RSS提要,现在我们可以向它添加一个条目。在下面的例子中,我们使用SyndEntryImpl的SyndEntry接口的默认实现来创建一个新条目。
SyndEntry entry = new SyndEntryImpl();
entry.setTitle("Entry title");
entry.setLink("http://www.somelink.com/entry1");
feed.setEntries(Arrays.asList(entry));
5. Adding a Description
5.添加描述
As our entry is pretty empty so far, let’s add a description for it. We can do this by using the default implementation SyndContentImpl of the SyndContent interface:
由于我们的条目到目前为止还是很空的,让我们为它添加一个描述。我们可以通过使用SyndContent接口的默认实现SyndContentImpl来完成这个任务。
SyndContent description = new SyndContentImpl();
description.setType("text/html");
description.setValue("First entry");
entry.setDescription(description);
With the setType method, we have specified that the content of our description will be a text or HTML.
通过setType方法,我们已经指定了我们的描述内容将是一个文本或HTML。
6. Adding a Category
6.添加一个类别
RSS entries are often classified into categories to simplify the task of finding entries that we are interested in. Let’s see how we can add a category to the entry using the default implementation SyndCategoryImpl of the SyndCategory interface:
RSS条目通常被分为不同的类别,以简化寻找我们感兴趣的条目的工作。让我们看看我们如何为条目添加一个类别使用SyndCategoryImpl接口的默认实现SyndCategory:。
List<SyndCategory> categories = new ArrayList<>();
SyndCategory category = new SyndCategoryImpl();
category.setName("Sophisticated category");
categories.add(category);
entry.setCategories(categories);
7. Publishing the Feed
7.发布饲料
We already have an RSS feed with an entry. Now we want to publish it. For the purpose of this article, by publishing, we mean writing the feed to a stream:
我们已经有一个带有条目的RSS提要。现在我们要发布它。在这篇文章中,我们所说的发布,是指将feed写入一个流。
Writer writer = new FileWriter("xyz.txt");
SyndFeedOutput syndFeedOutput = new SyndFeedOutput();
syndFeedOutput.output(feed, writer);
writer.close();
8. Reading an External Feed
8.阅读外部资料
We already know how to create a new feed, but sometimes we just need to connect to an existing one.
我们已经知道如何创建一个新的饲料,但有时我们只是需要连接到一个现有的饲料。
Let’s see how to read/load a feed, given its URL:
让我们看看如何读取/加载一个饲料,给定其URL。
URL feedSource = new URL("http://rssblog.whatisrss.com/feed/");
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedSource));
9. Conclusion
9.结论
In this article, we have shown how to create an RSS feed with some entries, how to publish the feed, and how to read external feeds.
在这篇文章中,我们已经展示了如何创建一个带有一些条目的RSS提要,如何发布提要,以及如何阅读外部提要。
As always, you can check out the examples provided in this article over on GitHub.
一如既往,你可以在GitHub上查看本文提供的例子。