public static PrevisaoMetereologica getWeatherByRegion(String region) {
try {
PrevisaoMetereologica previsao = new PrevisaoMetereologica();
LinkedHashSet<Forecast> forecasts = new LinkedHashSet<Forecast>();
URL url = new URL("http://www.google.com/ig/api?weather=Aveiro&hl=pt-pt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(in);
do{
int eventType = streamReader.next();
if( eventType == XMLStreamConstants.START_ELEMENT ){
//Current conditions
if( streamReader.getLocalName().equals("current_conditions") ){
previsao = readCurrentConditions(streamReader);
}
//Forecasts
else if( streamReader.getLocalName().equals("forecast_conditions") ){
forecasts.add( readForecast(streamReader) );
}
}
}while( streamReader.hasNext() );
previsao.setForecasts(forecasts);
return previsao;
} catch (XMLStreamException ex) {
Logger.getLogger(Weather.class.getName()).log(Level.SEVERE, null, ex);