javaでromeを使ってRSSを生成してみた
突然QMACloneにRSS配信機能をつけたくなったのでつけることにした。
RSSの生成を自前で行うのは正直しんどい。よってライブラリを探してみた。
探してみるといろいろと種類があるようなのだが、google検索で最も引っかかった件数の多かった「rome」を使うことにした。迷ったときにもgoogleは役に立つ。
romeは日本語の情報がかなり少なく、自分が探した中では1件しか引っかからなかった。
romeを使うときはrome-1.0.jarとjdom-1.1.jarの二つが必要になる。あらかじめダウンロードしてクラスパスを設定しておく。
romeでrssを生成するときの流れは
QMAClone中での実際のソースはこんな感じ
final List<SyndEntry> entries = new ArrayList<SyndEntry>(); final Date publishedData = new Date(); for (PacketProblemData problem : CachedDatabase.getInstance().getLastestProblems()) { final SyndEntry entry = new SyndEntryImpl(); final String problemReportSentence = problem.getProblemReportSentence(); entry.setTitle("問題番号:" + problem.problemId + " " + problemReportSentence.substring(0, 20) + "..."); entry.setLink("http://kishibe.dyndns.tv/qmaclone/"); entry.setPublishedDate(publishedData); final SyndContent description = new SyndContentImpl(); description.setType("text/plain"); description.setValue(problemReportSentence); entry.setDescription(description); entry.setAuthor(problem.creator); entries.add(entry); } SyndFeed feed = new SyndFeedImpl(); feed.setFeedType("rss_2.0"); feed.setTitle("QMAClone 最新投稿問題"); feed.setLink("http://kishibe.dyndns.tv/qmaclone/"); feed.setDescription("QMACloneの最新投稿問題をお知らせいたします。1時間毎の更新です。"); feed.setEntries(entries); feed.setEncoding("utf-8"); final SyndFeedOutput output = new SyndFeedOutput(); final StringWriter stringWriter = new StringWriter(); try { output.output(feed, stringWriter); } catch (Exception e) { e.printStackTrace(); } final byte[] bs = stringWriter.toString().getBytes("utf-8"); final OutputStream outputStream = response.getOutputStream(); outputStream.write(bs); outputStream.close();
お手軽にrssが生成できた。
・・・というわけでrss配信行ってみます。
[Java]RomeでLivedoor Blog用にAtomをFeed(フィード) | RwJ http://blog.mikuriya.biz/?s=rome
rome: ROME: RSS/Atom syndication and publishing tools https://rome.dev.java.net/
JDOM http://www.jdom.org/