Sponsored link
informa (RSS API)
JavaでRSSを扱うAPIです。
これはLGPLライセンス
です。
他にも僕の好きなApacheライセンスのrsslibj
にも期待したいです。
04.02.29に使ったときは依存性がうまく解決できませんでした。
RSS Version 0.91においてよく以下のエラーがでます。
2004/03/16 5:29:04 de.nava.informa.parsers.RSSParser parse
RSS Version 2.0では
2004/03/16 5:29:11 de.nava.informa.parsers.RSSParser parse
情報: Channel uses RSS root element (Version 2.0).
導入
まずは以下より最新版をダウンロードします。バイナリー版でいいでしょう。
http://informa.sourceforge.net/
あとはライブラリーのパスを通すだけです。
informa.jar;commons-logging.jar;jdom.jar;XercesImpl.jar
で動きます。
サンプル
これで出力したものはここにあります。
ちなみに、右側に出ている日記リストはこれで作成しています。
RSSをパースします。
ここでははてなのrssを試しています。
public static void readTest() {
try {
URL url = new URL("http://d.hatena.ne.jp/xucker/rss/");
ChannelIF channel = RSSParser.parse(new ChannelBuilder(), url);
System.out.println(channel.getTitle());
System.out.println(channel.getDescription());
Collection list = channel.getItems();
ItemIF[] items = (ItemIF[]) list.toArray(new ItemIF[0]);
for (int i = 0; i < items.length; i++) {
String title = items[i].getTitle();
System.out.println(title + " " + items[i].getLink());
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
RSSを書き出します。
public static void writeTest() {
ChannelBuilder builder = new ChannelBuilder();
ChannelIF newChannel = builder.createChannel("My Homepage");
System.out.println(newChannel.getTitle());
newChannel.setFormat(ChannelFormat.RSS_1_0);
newChannel.setLanguage("ja");
try {
//
newChannel.setSite(new URL("http://www.xucker.jpn.org"));
//
newChannel.setLocation(
new URL("http://www.xucker.jpn.org/xucker-utf-8.rdf"));
ItemIF item =
builder.createItem(
newChannel,
"title",
"description",
new URL("http://localhost"));
item.setDate(new Date());
newChannel.addItem(item);
//c:\tmp ディレクトリーが必要になります。
RSS_1_0_Exporter writer =
new RSS_1_0_Exporter(new File("c:\\tmp\\rss.xml"));
writer.write(newChannel);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
