天気情報を読み込む - かまたま日記2
天気情報を読み込む その2 - かまたま日記2
前回の追記でWeatherFormatterがエラーになった件ですが、やはりライブラリーが無かっただけのようでした。
結局サンプルソースを動かすのに必要だったライブラリーは以下になります。*1
- commons-collections-3.2.1.jar
- commons-lang-2.6.jar
- dom4j-2.0.0-ALPHA-2.jar
- jaxen-1.1.1.jar
- log4j-1.2.16.jar
- velocity-1.7.jar
作ったソース(の一部)をさらしてみます。
なおHeadFirstのソースは以下のサイトでダウンロードできます。
Head First Code Downloads | Wickedly Smart
/** * 実行クラス * */ public class WeatherStation { public static void main(String[] args) throws Exception { // Log4jを作ります PropertyConfigurator.configure(WeatherStation.class.getClassLoader() .getResource("log4j.properties")); WeatherData weatherData = new WeatherData(); CurrentConditionDisplay currentDisp = new CurrentConditionDisplay(weatherData); StatisticsDisplay statisticsDisp = new StatisticsDisplay(weatherData); ForecastDisplay forecastDisp = new ForecastDisplay(weatherData); WeatherStation weatherStation = new WeatherStation(); WeatherForHeadFirst weather = weatherStation.getWeatherData(); weatherData.setMeasurements( Float.parseFloat(weather.getTemperature()) , Float.parseFloat(weather.getHumidity()) , Float.parseFloat(weather.getPressure()) ); } /** 船橋市のZIPCODE */ public static String ZIPCODE = "JAXX0011"; public WeatherForHeadFirst getWeatherData() throws Exception { // Yahoo! WeatherのXMLデータを取得します InputStream dataIn = new YahooRetrieverForHeadFirst().retrieve(ZIPCODE); // XMLデータをパースします return new YahooPaeserForHeadFirst().parse(dataIn); } }
public class YahooRetrieverForHeadFirst { private static Logger log = Logger.getLogger(YahooRetrieverForHeadFirst.class); /** * * @param zipcode * @return * @throws Exception */ public InputStream retrieve(String zipcode) throws Exception { log.info("お天気情報取得"); String url = "http://weather.yahooapis.com/forecastrss?p=" + zipcode + "&u=c"; URLConnection conn = new URL(url).openConnection(); return conn.getInputStream(); } }
public class YahooPaeserForHeadFirst { private static Logger log = Logger.getLogger(YahooPaeserForHeadFirst.class); public WeatherForHeadFirst parse(InputStream inputStream) throws Exception { WeatherForHeadFirst weather = new WeatherForHeadFirst(); log.info("XMLリーダー生成"); SAXReader xmlReader = createXmlReader(); Document doc = xmlReader.read(inputStream); log.info("XMLレスポンスをパースします。"); weather.setTemperature( doc.valueOf("/rss/channel/item/y:condition/@temp") ); weather.setPressure( doc.valueOf("/rss/channel/y:atmosphere/@pressure") ); weather.setHumidity( doc.valueOf("/rss/channel/y:atmosphere/@humidity") ); return weather; } private SAXReader createXmlReader() { Map<String,String> uris = new HashMap<String,String>(); uris.put("y","http://xml.weather.yahoo.com/ns/rss/1.0" ); uris.put("g","http://www.w3.org/2003/01/geo/wgs84_pos#" ); DocumentFactory factory = new DocumentFactory(); factory.setXPathNamespaceURIs(uris); SAXReader xmlReader = new SAXReader(); xmlReader.setDocumentFactory(factory); return xmlReader; } }
-
- -
今回参考にしたサイト
1. Velocityの概要 | TECHSCORE(テックスコア)
xmlns属性
http://d.hatena.ne.jp/t_yano/20070128/1169978117
*1:ライブラリーのバージョンはググって一番最初に出てきたものです。