packagecom.ljq.sax;importjava.util.ArrayList;importjava.util.List;importorg.xml.sax.Attributes;importorg.xml.sax.SAXException;importorg.xml.sax.helpers.DefaultHandler;importcom.ljq.entity.Forecast;importcom.ljq.entity.Weather;publicclassWeatherSaxextendsDefaultHandler {privateWeather weather;privateForecast forecast;privateListforecasts;privateString preTag;
@OverridepublicvoidstartDocument()throwsSAXException {
weather=newWeather();
forecasts=newArrayList();
}
@Overridepublicvoidcharacters(char[] ch,intstart,intlength)throwsSAXException {
}
@OverridepublicvoidstartElement(String uri, String localName, String name,
Attributes attr)throwsSAXException {if("city".equals(name)) {
weather.setCity(attr.getValue("data"));//等价于weather.setCity(attr.getValue("data"));}if("forecast_date".equals(name)) {
weather.setForecase_date(attr.getValue("data"));
}if("current_date_time".equals(name)) {
weather.setCurrent_date_time(attr.getValue("data"));
}if("current_conditions".equals(name)){
preTag=name;
}if("condition".equals(name)&&"current_conditions".equals(preTag)) {
weather.setCurrent_condition(attr.getValue("data"));
}if("humidity".equals(name)) {
weather.setCurrent_humidity(attr.getValue("data"));
}if("icon".equals(name)&&"current_conditions".equals(preTag)) {
weather.setCurrent_image_url(attr.getValue("data"));
}if("wind_condition".equals(name)) {
weather.setCurrent_wind(attr.getValue("data"));
}if("forecast_conditions".equals(name)) {
preTag=name;//记录标识,用来区分相同节点的不同父节点forecast=newForecast();
}if("day_of_week".equals(name)) {
forecast.setDay_of_week(attr.getValue("data"));
}if("low".equals(name)) {
forecast.setLow(attr.getValue("data"));
}if("high".equals(name)) {
forecast.setHigh(attr.getValue("data"));
}if("icon".equals(name)&&"forecast_conditions".equals(preTag)) {
forecast.setImage_url(attr.getValue("data"));
}if("condition".equals(name)&&"forecast_conditions".equals(preTag)) {
forecast.setCondition(attr.getValue("data"));
}
}
@OverridepublicvoidendElement(String uri, String localName, String name)throwsSAXException {if("forecast_conditions".equals(name)) {
forecasts.add(forecast);
forecast=null;
}if("weather".equals(name)) {
weather.setForecasts(forecasts);
}
}publicWeather getWeather() {returnweather;
}
}