JAVA開發RSS訂閱器

一、首先介紹什麼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();
        }

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章