Java Reference
In-Depth Information
POJOs are simple containers for data, so the constructors, getter and setter methods, and
any necessary overrides are mostly clutter. I can therefore simplify my life if I use a POGO
instead, as shown in the following listing.
Listing 5.9. Weather.groovy , a POGO to hold weather results from the web service
package mjg
class Weather {
String city
String region
String country
String condition
String temp
String chill
String humidity
String toString() {
"""
Weather for $city, $region, $country:
Condition : $condition
Temperature: $temp
Wind Chill : $chill
Humidity : $humidity
"""
}
}
The toString method is a way to produce formatted output. Groovy's multiline string
makes it particularly easy.
The other class I need is a parser for the web service. Because all I need is a GET request
I can use the parse method in the XmlSlurper class as usual and drill down the result-
ing DOM tree to get the results I want. That's pretty simple, too, as shown in the following
listing.
Listing 5.10. YahooParser.groovy , which accesses and parses the weather service
package mjg
class YahooParser {
final static String BASE = 'http://weather.yahooapis.com/forecastrss?'
Weather getWeather(String woeid) {
def root = new XmlSlurper().parse(BASE + "w=$woeid")
Weather w = new Weather(
Search WWH ::




Custom Search