Java 用フィード操作ライブラリの ROME とその拡張ライブラリの GeoRSS module で位置情報を含む GeoRSS を出力するサンプル。
使うライブラリ:
- ROME v1.0 RC1 Release (Jul/16/2008)
- GeoRSS module 0.9.8 (2007 June 6)
- JDOM 1.0
サンプルでは以下の5種類のRSSを出力する。
-GeoRSS 無し
-GeoRSS-Simple
-GeoRSS GML
-W3C Geo (W3C Basic Geo Vocabulary)
-GeoRSS Simple, GML, W3C Geo のすべて
ソースコード
import java.io.*;
import java.util.*;
import com.sun.syndication.io.*;
import com.sun.syndication.feed.synd.*;
import com.sun.syndication.feed.module.georss.*;
import com.sun.syndication.feed.module.georss.geometries.*;
public class GeoRssSample {
public static void main(String[] args) throws Exception {
System.out.println("----- sample_rome -----");
sample_rome();
System.out.println("----- sample_georss_simple -----");
sample_georss_simple();
System.out.println("----- sample_georss_gml -----");
sample_georss_gml();
System.out.println("----- sample_georss_w3c -----");
sample_georss_w3c();
System.out.println("----- sample_georss_all -----");
sample_georss_all();
}
private static void sample_rome() throws Exception {
// フィードを生成
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("たいとる: sample_rome");
feed.setDescription("説明");
feed.setLink("http://www.nilab.info/");
// エントリを生成
SyndEntry entry = new SyndEntryImpl();
entry.setTitle("エントリのタイトル");
// コンテンツを生成
SyndContent content = new SyndContentImpl();
content.setType("text/html");
content.setValue("エントリの内容: <a href=\"http://www.nilab.info/\">NI-Lab.</a>");
// コンテンツをエントリに追加
ArrayList<SyndContent> contents = new ArrayList<SyndContent>();
contents.add(content);
entry.setContents(contents);
// エントリをフィードに追加
ArrayList<SyndEntry> entries = new ArrayList<SyndEntry>();
entries.add(entry);
feed.setEntries(entries);
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, new PrintWriter(System.out));
}
private static void sample_georss_simple() throws Exception {
// フィードを生成
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("たいとる: sample_georss_simple");
feed.setDescription("説明");
feed.setLink("http://www.nilab.info/");
// エントリを生成
SyndEntry entry = new SyndEntryImpl();
entry.setTitle("エントリのタイトル: 名古屋城");
// コンテンツを生成
SyndContent content = new SyndContentImpl();
content.setType("text/html");
content.setValue("エントリの内容: <a href=\"http://www.nilab.info/\">NI-Lab.</a>");
// コンテンツをエントリに追加
ArrayList<SyndContent> contents = new ArrayList<SyndContent>();
contents.add(content);
entry.setContents(contents);
// GeoRSS要素を追加
GeoRSSModule georss = new SimpleModuleImpl();
georss.setPosition(new Position(35.18555, 136.899068));
List modules = entry.getModules();
modules.add(georss);
// エントリをフィードに追加
ArrayList<SyndEntry> entries = new ArrayList<SyndEntry>();
entries.add(entry);
feed.setEntries(entries);
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, new PrintWriter(System.out));
}
private static void sample_georss_gml() throws Exception {
// フィードを生成
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("たいとる: sample_georss_gml");
feed.setDescription("説明");
feed.setLink("http://www.nilab.info/");
// エントリを生成
SyndEntry entry = new SyndEntryImpl();
entry.setTitle("エントリのタイトル: 名古屋城");
// コンテンツを生成
SyndContent content = new SyndContentImpl();
content.setType("text/html");
content.setValue("エントリの内容: <a href=\"http://www.nilab.info/\">NI-Lab.</a>");
// コンテンツをエントリに追加
ArrayList<SyndContent> contents = new ArrayList<SyndContent>();
contents.add(content);
entry.setContents(contents);
// GeoRSS要素を追加
GeoRSSModule georss = new GMLModuleImpl();
georss.setPosition(new Position(35.18555, 136.899068));
List modules = entry.getModules();
modules.add(georss);
// エントリをフィードに追加
ArrayList<SyndEntry> entries = new ArrayList<SyndEntry>();
entries.add(entry);
feed.setEntries(entries);
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, new PrintWriter(System.out));
}
private static void sample_georss_w3c() throws Exception {
// フィードを生成
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("たいとる: sample_georss_w3c");
feed.setDescription("説明");
feed.setLink("http://www.nilab.info/");
// エントリを生成
SyndEntry entry = new SyndEntryImpl();
entry.setTitle("エントリのタイトル: 名古屋城");
// コンテンツを生成
SyndContent content = new SyndContentImpl();
content.setType("text/html");
content.setValue("エントリの内容: <a href=\"http://www.nilab.info/\">NI-Lab.</a>");
// コンテンツをエントリに追加
ArrayList<SyndContent> contents = new ArrayList<SyndContent>();
contents.add(content);
entry.setContents(contents);
// GeoRSS要素を追加
GeoRSSModule georss = new W3CGeoModuleImpl();
georss.setPosition(new Position(35.18555, 136.899068));
List modules = entry.getModules();
modules.add(georss);
// エントリをフィードに追加
ArrayList<SyndEntry> entries = new ArrayList<SyndEntry>();
entries.add(entry);
feed.setEntries(entries);
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, new PrintWriter(System.out));
}
private static void sample_georss_all() throws Exception {
// フィードを生成
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("たいとる: sample_georss_all");
feed.setDescription("説明");
feed.setLink("http://www.nilab.info/");
// エントリを生成
SyndEntry entry = new SyndEntryImpl();
entry.setTitle("エントリのタイトル: 名古屋城");
// コンテンツを生成
SyndContent content = new SyndContentImpl();
content.setType("text/html");
content.setValue("エントリの内容: <a href=\"http://www.nilab.info/\">NI-Lab.</a>");
// コンテンツをエントリに追加
ArrayList<SyndContent> contents = new ArrayList<SyndContent>();
contents.add(content);
entry.setContents(contents);
// GeoRSS要素を追加
GeoRSSModule georss_simple = new SimpleModuleImpl();
GeoRSSModule georss_gml = new GMLModuleImpl();
GeoRSSModule georss_w3c = new W3CGeoModuleImpl();
georss_simple.setPosition(new Position(35.18555, 136.899068));
georss_gml. setPosition(new Position(35.18555, 136.899068));
georss_w3c. setPosition(new Position(35.18555, 136.899068));
List modules = entry.getModules();
modules.add(georss_simple);
modules.add(georss_gml);
modules.add(georss_w3c);
// エントリをフィードに追加
ArrayList<SyndEntry> entries = new ArrayList<SyndEntry>();
entries.add(entry);
feed.setEntries(entries);
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, new PrintWriter(System.out));
}
}
出力結果
----- sample_rome -----
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>たいとる: sample_rome</title>
<link>http://www.nilab.info/</link>
<description>説明</description>
<item>
<title>エントリのタイトル</title>
<content:encoded>エントリの内容: <a href="http://www.nilab.info/">NI-Lab.</a></content:encoded>
</item>
</channel>
</rss>
----- sample_georss_simple -----
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:georss="http://www.georss.org/georss" version="2.0">
<channel>
<title>たいとる: sample_georss_simple</title>
<link>http://www.nilab.info/</link>
<description>説明</description>
<item>
<title>エントリのタイトル: 名古屋城</title>
<content:encoded>エントリの内容: <a href="http://www.nilab.info/">NI-Lab.</a></content:encoded>
<georss:point>35.18555 136.899068</georss:point>
</item>
</channel>
</rss>
----- sample_georss_gml -----
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" version="2.0">
<channel>
<title>たいとる: sample_georss_gml</title>
<link>http://www.nilab.info/</link>
<description>説明</description>
<item>
<title>エントリのタイトル: 名古屋城</title>
<content:encoded>エントリの内容: <a href="http://www.nilab.info/">NI-Lab.</a></content:encoded>
<georss:where>
<gml:Point>
<gml:pos>35.18555 136.899068</gml:pos>
</gml:Point>
</georss:where>
</item>
</channel>
</rss>
----- sample_georss_w3c -----
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
<channel>
<title>たいとる: sample_georss_w3c</title>
<link>http://www.nilab.info/</link>
<description>説明</description>
<item>
<title>エントリのタイトル: 名古屋城</title>
<content:encoded>エントリの内容: <a href="http://www.nilab.info/">NI-Lab.</a></content:encoded>
<geo:lat>35.18555</geo:lat>
<geo:long>136.899068</geo:long>
</item>
</channel>
</rss>
----- sample_georss_all -----
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:gml="http://www.opengis.net/gml" version="2.0">
<channel>
<title>たいとる: sample_georss_all</title>
<link>http://www.nilab.info/</link>
<description>説明</description>
<item>
<title>エントリのタイトル: 名古屋城</title>
<content:encoded>エントリの内容: <a href="http://www.nilab.info/">NI-Lab.</a></content:encoded>
<georss:point>35.18555 136.899068</georss:point>
<georss:where>
<gml:Point>
<gml:pos>35.18555 136.899068</gml:pos>
</gml:Point>
</georss:where>
<geo:lat>35.18555</geo:lat>
<geo:long>136.899068</geo:long>
</item>
</channel>
</rss>
Ref.
- ROME: RSS/Atom syndication and publishing tools
- GeoRSS goes Rome: GeoRSS module for the java rss library 'rome'
- JDOM
- GeoRSS | GeoRSS :: Geographically Encoded Objects for RSS feeds - GeoRSS-Simple - GeoRSS GML - W3C Geo
tags: zlashdot GIS Feed GeoRSS Java
Posted by NI-Lab. (@nilab)