sax解析xml属性配对法.docx

上传人:b****8 文档编号:9378465 上传时间:2023-02-04 格式:DOCX 页数:17 大小:19KB
下载 相关 举报
sax解析xml属性配对法.docx_第1页
第1页 / 共17页
sax解析xml属性配对法.docx_第2页
第2页 / 共17页
sax解析xml属性配对法.docx_第3页
第3页 / 共17页
sax解析xml属性配对法.docx_第4页
第4页 / 共17页
sax解析xml属性配对法.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

sax解析xml属性配对法.docx

《sax解析xml属性配对法.docx》由会员分享,可在线阅读,更多相关《sax解析xml属性配对法.docx(17页珍藏版)》请在冰豆网上搜索。

sax解析xml属性配对法.docx

sax解析xml属性配对法

sax解析xml(属性配对法)

student.xml文件

xmlversion="1.0"encoding="UTF-8"?

>

赵海波

Spring整合开发

85

轻量级J2EE应用开发

95

Ajax应用开发

80

程卫娜

Spring整合开发

80

轻量级J2EE应用开发

85

Ajax应用开发

90

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;

/**所学课程**/

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;//记录

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1