东北、风速:
1米/秒"/>
学生Student类
packagecom.ljq.entity;
importjava.util.Set;
/**
*学生信息表
*
*@authorjiqinlin
*
*/
publicclassStudent{
/**姓名**/
privateStringname;
/**性别**/
privateStringsex;
/**所学课程**/
privateSetlessons;
publicStudent(){
}
publicStudent(Stringname,Stringsex,Setlessons){
this.name=name;
this.sex=sex;
this.lessons=lessons;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetSex(){
returnsex;
}
publicvoidsetSex(Stringsex){
this.sex=sex;
}
publicSetgetLessons(){
returnlessons;
}
publicvoidsetLessons(Setlessons){
this.lessons=lessons;
}
}
课程Lesson类
packagecom.ljq.entity;
/**
*课程
*
*@authorjiqinlin
*
*/
publicclassLesson{
/**课程名称**/
privateStringlessonName;
/**课程成绩**/
privateintlessonScore;
publicLesson(){
}
publicLesson(StringlessonName,intlessonScore){
this.lessonName=lessonName;
this.lessonScore=lessonScore;
}
publicStringgetLessonName(){
returnlessonName;
}
publicvoidsetLessonName(StringlessonName){
this.lessonName=lessonName;
}
publicintgetLessonScore(){
returnlessonScore;
}
publicvoidsetLessonScore(intlessonScore){
this.lessonScore=lessonScore;
}
}
当前天气信息的类Weather
packagecom.ljq.entity;
importjava.util.List;
/**
*当前天气信息的类
*
*@authorjiqinlin
*
*/
publicclassWeather{
/**城市**/
privateStringcity;
/**当天日期,格式为yyyy-mm-dd**/
privateStringforecase_date;
/**当前时间**/
privateStringcurrent_date_time;
/**现象描述**/
privateStringcurrent_condition;
/**当前干燥程度**/
privateStringcurrent_humidity;
/**当前图片地址**/
privateStringcurrent_image_url;
/**风向**/
privateStringcurrent_wind;
/**此处只能用有序的List集合,因为第一位索引表示当天的天气情况**/
privateListforecasts;
publicStringgetCity(){
returncity;
}
publicvoidsetCity(Stringcity){
this.city=city;
}
publicStringgetForecase_date(){
returnforecase_date;
}
publicvoidsetForecase_date(Stringforecase_date){
this.forecase_date=forecase_date;
}
publicStringgetCurrent_date_time(){
returncurrent_date_time;
}
publicvoidsetCurrent_date_time(Stringcurrent_date_time){
this.current_date_time=current_date_time;
}
publicStringgetCurrent_condition(){
returncurrent_condition;
}
publicvoidsetCurrent_condition(Stringcurrent_condition){
this.current_condition=current_condition;
}
publicStringgetCurrent_humidity(){
returncurrent_humidity;
}
publicvoidsetCurrent_humidity(Stringcurrent_humidity){
this.current_humidity=current_humidity;
}
publicStringgetCurrent_image_url(){
returncurrent_image_url;
}
publicvoidsetCurrent_image_url(Stringcurrent_image_url){
this.current_image_url=current_image_url;
}
publicStringgetCurrent_wind(){
returncurrent_wind;
}
publicvoidsetCurrent_wind(Stringcurrent_wind){
this.current_wind=current_wind;
}
publicListgetForecasts(){
returnforecasts;
}
publicvoidsetForecasts(Listforecasts){
this.forecasts=forecasts;
}
}
未来天气信息的类Forecast
packagecom.ljq.entity;
/**
*未来天气信息的类
*
*@authorjiqinlin
*
*/
publicclassForecast{
/**星期几**/
privateStringday_of_week;
/**最低温度**/
privateStringlow;
/**最高温度**/
privateStringhigh;
/**图片地址**/
privateStringimage_url;
/**现象描述**/
privateStringcondition;
publicStringgetDay_of_week(){
returnday_of_week;
}
publicvoidsetDay_of_week(Stringday_of_week){
this.day_of_week=day_of_week;
}
publicStringgetLow(){
returnlow;
}
publicvoidsetLow(Stringlow){
this.low=low;
}
publicStringgetHigh(){
returnhigh;
}
publicvoidsetHigh(Stringhigh){
this.high=high;
}
publicStringgetImage_url(){
returnimage_url;
}
publicvoidsetImage_url(Stringimage_url){
this.image_url=image_url;
}
publicStringgetCondition(){
returncondition;
}
publicvoidsetCondition(Stringcondition){
this.condition=condition;
}
}
StudentSax解析
packagecom.ljq.sax;
importjava.util.HashSet;
importjava.util.Set;
importorg.xml.sax.Attributes;
importorg.xml.sax.SAXException;
importorg.xml.sax.helpers.DefaultHandler;
importcom.ljq.entity.Lesson;
importcom.ljq.entity.Student;
publicclassStudentSaxextendsDefaultHandler{
privateLessonlesson;
privateSetlessons;
privateStudentstudent;
privateSetstudents;
privateStringpreTag;
@Override
publicvoidstartDocument()throwsSAXException{
lessons=newHashSet();
students=newHashSet();
}
@Override
publicvoidcharacters(char[]ch,intstart,intlength)
throwsSAXException{
if(student!
=null){
Stringdata=newString(ch,start,length);
if("name".equals(preTag)){
student.setName(data);
}
if("sex".equals(preTag)){
student.setSex(data);
}
if("lessonName".equals(preTag)){
lesson.setLessonName(data);
}
if("lessonScore".equals(preTag)){
lesson.setLessonScore(Integer.parseInt(data));
}
}
}
@Override
publicvoidstartElement(Stringuri,StringlocalName,Stringname,
Attributesattr)throwsSAXException{
if("student".equals(name)){
student=newStudent();
}
if("lesson".equals(name)){
lesson=newLesson();
}
preTag=name;
}
@Override
publicvoidendElement(Stringuri,StringlocalName,Stringname)
throwsSAXException{
if(student!
=null&&"student".equals(name)){
student.setLessons(lessons);
students.add(student);
student=null;
lessons=newHashSet();
}
if(lesson!
=null&&"lesson".equals(name)){
lessons.add(lesson);
lesson=null;
}
preTag=null;
}
publicSetgetStudents(){
returnstudents;
}
publicSetgetLessons(){
returnlessons;
}
}
WeatherSax解析
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{
privateWeatherweather;
privateForecastforecast;
privateListforecasts;
privateStringpreTag;
@Override
publicvoidstartDocument()throwsSAXException{
weather=newWeather();
forecasts=newArrayList();
}
@Override
publicvoidcharacters(char[]ch,intstart,intlength)
throwsSAXException{
}
@Override
publicvoidstartElement(Stringuri,StringlocalName,Stringname,
Attributesattr)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;//记录