LoginSignup
17
23

More than 5 years have passed since last update.

簡易RSSリーダーを作ってみた 〜 Java編 〜

Last updated at Posted at 2015-05-30

Cで書いた簡易RSSリーダーと同じ機能のものをJava逆引きレシピを参考にして、Javaでも書いてみました。JavaのXMLパーサーは、URLを直接指定できたりするので、RSSフィードをダウンロードする処理を書く必要がありませんでした。

サンプルコード

DocumentBuilderでアップルのRSSフィードを取得して、サマリを表示します。

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class getrss {

    public static void main(String[] args) {
        String path = "http://www.apple.com/jp/main/rss/hotnews/hotnews.rss";
        parseXML(path);
    }

    public static void parseXML(String path) {
        try {
            DocumentBuilderFactory  factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder         builder = factory.newDocumentBuilder();
            Document                document = builder.parse(path);
            Element                 root = document.getDocumentElement();

            /* Get and print Title of RSS Feed. */
            NodeList                channel = root.getElementsByTagName("channel");
            NodeList                title = ((Element)channel.item(0)).getElementsByTagName("title");
            System.out.println("\nTitle: " + title.item(0).getFirstChild().getNodeValue() + "\n");

            /* Get Node list of RSS items */
            NodeList                item_list = root.getElementsByTagName("item");
            for (int i = 0; i <item_list.getLength(); i++) {
                Element  element = (Element)item_list.item(i);
                NodeList item_title = element.getElementsByTagName("title");
                NodeList item_link  = element.getElementsByTagName("link");
                System.out.println(" title: " + item_title.item(0).getFirstChild().getNodeValue());
                System.out.println(" link:  " + item_link.item(0).getFirstChild().getNodeValue() + "\n");
            }
        } catch (IOException e) {
            System.out.println("IO Exception");
        } catch (ParserConfigurationException e) {
            System.out.println("Parser Configuration Exception");
        } catch (SAXException e) {
            System.out.println("SAX Exception");
        }
        return;
    }
}

実行結果

「アップル - ホットニュース」のサマリが表示されました。

$ javac getrss.java
$ java getrss

Title: アップル - ホットニュース

 title: Apple、感圧タッチトラックパッドを搭載した15インチのMacBook Pro、238,800円の新しいiMac Retina 5Kディスプレイモデルを発売
 link:  http://www.apple.com/jp/pr/library/2015/05/19Apple-Introduces-15-inch-MacBook-Pro-with-Force-Touch-Trackpad-New-1-999-iMac-with-Retina-5K-Display.html

 title: 日本郵政グループ、IBM、Apple、日本の高齢者がサービスを通じて家族・地域コミュニティーとつながるために、iPadと専用アプリケーションを提供
 link:  http://www.apple.com/jp/pr/library/2015/04/30Japan-Post-Group-IBM-and-Apple-Deliver-iPads-and-Custom-Apps-to-Connect-Elderly-in-Japan-to-Services-Family-and-Community.html

 title: Apple、第2四半期の業績を発表
 link:  http://www.apple.com/jp/pr/library/2015/04/27Apple-Reports-Record-Second-Quarter-Results.html

    ・
    ・
    ・
17
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
23