如何使用Java订阅RSS Feed?


RSS 提要是从流行网站获取最新新闻文章的常用方法之一。
在本博客中,我们将使用 java 库rome库通过 rss 提要从技术博客网站获取文章。

我们的用例是从流行的博客页面(如https://www.jdon.com/rss等)获取 RSS 提要。

客户端
现在我们有了 rss feed url,我们可以利用它的优势通过拉取文章来阅读它。
我们将首先在 IDE 中创建项目 RSSReaderClient。然后第一个任务是将Roma库添加到 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>RSSReaderClient</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>rome</groupId>
            <artifactId>rome</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>

POJO 存储新闻文章

  • 让我们创建 NewsArticle POJO 来存储文章数据。我们稍后会需要它。

import java.util.List;

public class NewsArticle {
    private String title;
    private String link;
    private String imgUrl;
    private List<String> categories;

    public String getPublishedDate() {
        return publishedDate;
    }

    public void setPublishedDate(String publishedDate) {
        this.publishedDate = publishedDate;
    }

    private String publishedDate;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public List<String> getCategories() {
        return categories;
    }

    public void setCategories(List<String> categories) {
        this.categories = categories;
    }

    @Override
    public String toString() {
        return "NewsArticle{" +
                "title='" + title + '\'' +
                ", link='" + link + '\'' +
                ", imgUrl='" + imgUrl + '\'' +
                ", categories=" + categories +
                ", publishedDate='" + publishedDate + '\'' +
                '}';
    }
}

RSS 阅读器的实用程序类

  • 此外,我们将创建实用程序类来执行读取 rss 提要操作,然后将结果映射到我们之前创建的 NewsArticle POJO。

import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RSSReaderUtil {

    public static List<NewsArticle> read(String feedUrl) throws IOException, FeedException {
        URL feedSource = new URL(feedUrl);
        SyndFeedInput input = new SyndFeedInput();
        SyndFeed feed = input.build(new XmlReader(feedSource));
        Iterator itr = feed.getEntries().iterator();
        List<NewsArticle> results = new ArrayList<>();
        while (itr.hasNext()) {
            SyndEntry syndEntry = (SyndEntry) itr.next();
            results.add(mapToArticle(syndEntry));
        }

        return results;
    }

    /**
     * Map to Article
     * @param syndEntry
     */
    private static NewsArticle mapToArticle(SyndEntry syndEntry) {
        NewsArticle newsArticle = new NewsArticle();
        newsArticle.setTitle(syndEntry.getTitle());
        newsArticle.setPublishedDate(syndEntry.getPublishedDate().toString());
        newsArticle.setImgUrl(
"");
        newsArticle.setLink(syndEntry.getLink());
        return newsArticle;
    }
}

读客户端

  • 一旦我们准备好实用程序,那么我们所要做的就是在我们的客户端中使用它。
  • 我们有我们将定位并从中提取文章的提要列表
  • 我们将传递给它我们的 RssReaderUtility.read() 方法,它将返回 List<NewsArticle> 的响应
  • 一旦我们得到响应,我们所要做的就是打印它。

import com.rometools.rome.io.FeedException;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

public class RSSReader {

    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException, FeedException {
        URL feedSource = new URL("https://www.jdon.com/rss");
        List<String> targetFeedsList = List.of(
"https://www.jdon.com/rss",
               
"https://eng.uber.com/feed/",
               
"https://eng.lyft.com/feed",
               
"https://netflixtechblog.com/feed");

        for(String url : targetFeedsList){
            List<NewsArticle> results = RSSReaderUtil.read(url);
            System.out.println(
"url : "+url);
            results.stream().forEach(a-> System.out.println(a.toString()));
            System.out.println(
"==========");
        }
    }
}

测试客户端

  • 让我们运行我们的 java 应用程序,按照我们的逻辑,我们的阅读器将读取提要列表并打印它。
  • 正如您在输出的屏幕截图中所见,我们拥有来自谷歌云博客的所有最新 rss 提要。