JAVA开发RSS订阅器

本文介绍了RSS(简易信息聚合)的基本概念及其在信息共享中的作用,并深入探讨了如何使用SpringBoot结合ROME框架进行RSS订阅的开发。文章详细展示了如何配置依赖、编写代码来实现RSS的读取和解析,为开发者提供了实用的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、首先介绍什么RSS、ROME。

RSS:

RSS订阅是站点用来和其他站点之间共享内容的一种简易方式,即Really Simple Syndication(简易信息聚合)。

RSS以其方便快捷的工作方式,为广大网编带了工作效率的跨越,但是也助长了信息高速重复。

ROME:

Rome是为RSS聚合而开发的一个框架,让你可以快速的开发基于java的RSS阅读。

二、使用Springboot开发RSS订阅

1、导入jar包,配置pom.xml

        <dependency>
            <groupId>com.rometools</groupId>
            <artifactId>rome</artifactId>
            <version>1.8.0</version>
        </dependency>

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

2、编写代码

package com.citydo.rss.utils;

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

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;

public class FeedConsumer {

    private final static String RSS_URL = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=%s&type=&dateb=&owner=exclude&count=&output=atom";

    public void  test() throws MalformedURLException {
        //URL feedUrl = new URL(String.format(RSS_URL, symbol));
        //null 代表header
        URL feedUrl = new URL(String.format(RSS_URL, null));
        SyndFeedInput input = new SyndFeedInput();
        SyndFeed feed = null;
        try {
            feed = input.build(new XmlReader(feedUrl));
        }catch(Exception e) {
            e.printStackTrace();
        }
        if (feed == null) {
            //log.warn("syndFeed is null, symbol:{}", symbol);
            return;
        }
        List<SyndEntry> list = feed.getEntries();
        for (SyndEntry entry : list) {
            System.out.println(entry.getTitle() + "-"+ entry.getUpdatedDate() + "-" + entry.getLink());
        }

    }


    public static void main(String[] args) {
        try {
            String url = "http://localhost:8080/rss";

            try (XmlReader reader = new XmlReader(new URL(url))) {
                SyndFeed feed = new SyndFeedInput().build(reader);
                System.out.println(feed.getTitle());
                System.out.println("***********************************");
                for (SyndEntry entry : feed.getEntries()) {
                    System.out.println(entry);
                    System.out.println("***********************************");
                }
                System.out.println("Done");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值