sax解析xml属性配对法.docx
- 文档编号:9378465
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:17
- 大小:19KB
sax解析xml属性配对法.docx
《sax解析xml属性配对法.docx》由会员分享,可在线阅读,更多相关《sax解析xml属性配对法.docx(17页珍藏版)》请在冰豆网上搜索。
sax解析xml属性配对法
sax解析xml(属性配对法)
student.xml文件
xmlversion="1.0"encoding="UTF-8"?
>
fuzhou_weather.xml文件
xmlversion="1.0"encoding="UTF-8"?
>
--福州的天气情况-->
row="0"section="0"> 00: 00+0000"/> 43%"/> 东北、风速: 1米/秒"/> 学生Student类 packagecom.ljq.entity; importjava.util.Set; /** *学生信息表 * *@authorjiqinlin * */ publicclassStudent{ /**姓名**/ privateStringname; /**性别**/ privateStringsex; /**所学课程**/ privateSet publicStudent(){ } publicStudent(Stringname,Stringsex,Set this.name=name; this.sex=sex; this.lessons=lessons; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetSex(){ returnsex; } publicvoidsetSex(Stringsex){ this.sex=sex; } publicSet returnlessons; } publicvoidsetLessons(Set 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集合,因为第一位索引表示当天的天气情况**/ privateList 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; } publicList returnforecasts; } publicvoidsetForecasts(List 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; privateSet privateStudentstudent; privateSet 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; } publicSet returnstudents; } publicSet 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; privateList 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;//记录
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- sax 解析 xml 属性 配对