sax解析xml案例一
转自:http://www.cnblogs.com/linjiqin/archive/2011/03/11/1981076.html——————————————————————————————————————————————————————————————————————————————————————<?xml version="1.0" encoding="UTF-8"?><StudentInfo> <student> <name>赵海波</name> <sex>男</sex> <lesson> <lessonName>Spring整合开发</lessonName> <lessonScore>85</lessonScore> </lesson> <lesson> <lessonName>轻量级J2EE应用开发</lessonName> <lessonScore>95</lessonScore> </lesson> <lesson> <lessonName>Ajax应用开发</lessonName> <lessonScore>80</lessonScore> </lesson> </student> <student> <name>程卫娜</name> <sex>女</sex> <lesson> <lessonName>Spring整合开发</lessonName> <lessonScore>80</lessonScore> </lesson> <lesson> <lessonName>轻量级J2EE应用开发</lessonName> <lessonScore>85</lessonScore> </lesson> <lesson> <lessonName>Ajax应用开发</lessonName> <lessonScore>90</lessonScore> </lesson> </student> </StudentInfo>
fuzhou_weather.xml文件
<?xml version="1.0" encoding="UTF-8"?><!-- 福州的天气情况 --><xml_api_reply version="1"> <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"> <forecast_information> <city data="Fuzhou, Fujian" /> <postal_code data="fuzhou" /> <latitude_e6 data="" /> <longitude_e6 data="" /> <forecast_date data="2011-01-08" /> <current_date_time data="2011-01-08 23:00:00 +0000" /> <unit_system data="SI" /> </forecast_information> <current_conditions> <condition data="多云" /> <temp_f data="53" /> <temp_c data="12" /> <humidity data="湿度: 43%" /> <icon data="/ig/images/weather/mostly_cloudy.gif" /> <wind_condition data="风向: 东北、风速:1 米/秒" /> </current_conditions> <forecast_conditions> <day_of_week data="周六" /> <low data="7" /> <high data="14" /> <icon data="/ig/images/weather/chance_of_rain.gif" /> <condition data="可能有雨" /> </forecast_conditions> <forecast_conditions> <day_of_week data="周日" /> <low data="6" /> <high data="12" /> <icon data="/ig/images/weather/chance_of_rain.gif" /> <condition data="可能有雨" /> </forecast_conditions> <forecast_conditions> <day_of_week data="周一" /> <low data="5" /> <high data="10" /> <icon data="/ig/images/weather/mostly_sunny.gif" /> <condition data="晴间多云" /> </forecast_conditions> <forecast_conditions> <day_of_week data="周二" /> <low data="4" /> <high data="8" /> <icon data="/ig/images/weather/chance_of_rain.gif" /> <condition data="可能有雨" /> </forecast_conditions> </weather></xml_api_reply>
学生Student类
package com.ljq.entity; import java.util.Set; /** * 学生信息表 * * @author jiqinlin * */public class Student { /** 姓名 * */ private String name; /** 性别 * */ private String sex; /** 所学课程 * */ private Set<Lesson> lessons; public Student() { } public Student(String name, String sex, Set<Lesson> lessons) { this.name = name; this.sex = sex; this.lessons = lessons; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Set<Lesson> getLessons() { return lessons; } public void setLessons(Set<Lesson> lessons) { this.lessons = lessons; } }
课程Lesson类
package com.ljq.entity; /** * 课程 * * @author jiqinlin * */public class Lesson { /** 课程名称 * */ private String lessonName; /** 课程成绩 * */ private int lessonScore; public Lesson() { } public Lesson(String lessonName, int lessonScore) { this.lessonName = lessonName; this.lessonScore = lessonScore; } public String getLessonName() { return lessonName; } public void setLessonName(String lessonName) { this.lessonName = lessonName; } public int getLessonScore() { return lessonScore; } public void setLessonScore(int lessonScore) { this.lessonScore = lessonScore; } }
当前天气信息的类Weather
package com.ljq.entity; import java.util.List; /** * 当前天气信息的类 * * @author jiqinlin * */public class Weather { /** 城市 * */ private String city; /** 当天日期,格式为yyyy-mm-dd * */ private String forecase_date; /** 当前时间 * */ private String current_date_time; /** 现象描述 * */ private String current_condition; /** 当前干燥程度 * */ private String current_humidity; /** 当前图片地址 * */ private String current_image_url; /** 风向 * */ private String current_wind; /** 此处只能用有序的List集合,因为第一位索引表示当天的天气情况 **/ private List<Forecast> forecasts; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getForecase_date() { return forecase_date; } public void setForecase_date(String forecase_date) { this.forecase_date = forecase_date; } public String getCurrent_date_time() { return current_date_time; } public void setCurrent_date_time(String current_date_time) { this.current_date_time = current_date_time; } public String getCurrent_condition() { return current_condition; } public void setCurrent_condition(String current_condition) { this.current_condition = current_condition; } public String getCurrent_humidity() { return current_humidity; } public void setCurrent_humidity(String current_humidity) { this.current_humidity = current_humidity; } public String getCurrent_image_url() { return current_image_url; } public void setCurrent_image_url(String current_image_url) { this.current_image_url = current_image_url; } public String getCurrent_wind() { return current_wind; } public void setCurrent_wind(String current_wind) { this.current_wind = current_wind; } public List<Forecast> getForecasts() { return forecasts; } public void setForecasts(List<Forecast> forecasts) { this.forecasts = forecasts; } }
未来天气信息的类Forecast
package com.ljq.entity; /** * 未来天气信息的类 * * @author jiqinlin * */public class Forecast { /** 星期几 * */ private String day_of_week; /** 最低温度 * */ private String low; /** 最高温度 * */ private String high; /** 图片地址 * */ private String image_url; /** 现象描述 * */ private String condition; public String getDay_of_week() { return day_of_week; } public void setDay_of_week(String day_of_week) { this.day_of_week = day_of_week; } public String getLow() { return low; } public void setLow(String low) { this.low = low; } public String getHigh() { return high; } public void setHigh(String high) { this.high = high; } public String getImage_url() { return image_url; } public void setImage_url(String image_url) { this.image_url = image_url; } public String getCondition() { return condition; } public void setCondition(String condition) { this.condition = condition; } }
StudentSax解析
package com.ljq.sax; import java.util.HashSet;import java.util.Set; import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler; import com.ljq.entity.Lesson;import com.ljq.entity.Student; public class StudentSax extends DefaultHandler { private Lesson lesson; private Set<Lesson> lessons; private Student student; private Set<Student> students; private String preTag; @Override public void startDocument() throws SAXException { lessons = new HashSet<Lesson>(); students = new HashSet<Student>(); } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (student != null) { String data = new String(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 public void startElement(String uri, String localName, String name, Attributes attr) throws SAXException { if ("student".equals(name)) { student = new Student(); } if ("lesson".equals(name)) { lesson = new Lesson(); } preTag = name; } @Override public void endElement(String uri, String localName, String name) throws SAXException { if (student != null && "student".equals(name)) { student.setLessons(lessons); students.add(student); student = null; lessons = new HashSet<Lesson>(); } if (lesson != null && "lesson".equals(name)) { lessons.add(lesson); lesson = null; } preTag = null; } public Set<Student> getStudents() { return students; } public Set<Lesson> getLessons() { return lessons; }}
WeatherSax解析
package com.ljq.sax; import java.util.ArrayList;import java.util.List; import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler; import com.ljq.entity.Forecast;import com.ljq.entity.Weather; public class WeatherSax extends DefaultHandler { private Weather weather; private Forecast forecast; private List<Forecast> forecasts; private String preTag; @Override public void startDocument() throws SAXException { weather = new Weather(); forecasts = new ArrayList<Forecast>(); } @Override public void characters(char[] ch, int start, int length) throws SAXException { } @Override public void startElement(String uri, String localName, String name, Attributes attr) throws SAXException { 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 = new Forecast(); } 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")); } } @Override public void endElement(String uri, String localName, String name) throws SAXException { if ("forecast_conditions".equals(name)) { forecasts.add(forecast); forecast = null; } if ("weather".equals(name)) { weather.setForecasts(forecasts); } } public Weather getWeather() { return weather; }}
StudentSaxTest测试类
package com.ljq.test; import java.io.InputStream;import java.util.Set; import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory; import com.ljq.entity.Lesson;import com.ljq.entity.Student;import com.ljq.sax.StudentSax; public class StudentSaxTest { public static void main(String[] args) throws Exception { Set<Student> students = new StudentSaxTest().parseXMLFile(); for(Student stu : students){ System.out.println("name=" + stu.getName()); System.out.println("sex=" + stu.getSex()); Set<Lesson> lessons = stu.getLessons(); for(Lesson lesson : lessons){ System.out.println("LessonName = " + lesson.getLessonName()); System.out.println("LessonScore = " + lesson.getLessonScore()); System.out.println("---------------"); } System.out.println("=========="); } } // 解析文档 private Set<Student> parseXMLFile() throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); InputStream is = StudentSaxTest.class.getClassLoader() .getResourceAsStream("student.xml"); StudentSax handle = new StudentSax(); saxParser.parse(is, handle); is.close(); return handle.getStudents(); } }
WeatherSaxTest测试类
package com.ljq.test; import java.io.InputStream;import java.util.List; import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory; import com.ljq.entity.Forecast;import com.ljq.entity.Weather;import com.ljq.sax.WeatherSax; public class WeatherSaxTest { public static void main(String[] args) throws Exception{ Weather weather = new WeatherSaxTest().readXml(); System.out.println("city=" + weather.getCity()); System.out.println("forecase_date=" + weather.getForecase_date()); System.out.println("current_date_time=" + weather.getCurrent_date_time()); System.out.println("condition=" + weather.getCurrent_condition()); System.out.println("humidity=" + weather.getCurrent_humidity()); System.out.println("icon=" + weather.getCurrent_image_url()); System.out.println("wind_condition=" + weather.getCurrent_wind()); System.out.println("==========="); List<Forecast> forecasts = weather.getForecasts(); for(Forecast forecast : forecasts){ System.out.println("day_of_week=" + forecast.getDay_of_week()); System.out.println("low=" + forecast.getLow()); System.out.println("high=" + forecast.getHigh()); System.out.println("icon=" + forecast.getImage_url()); System.out.println("condition=" + forecast.getCondition()); System.out.println("---------------"); } } private Weather readXml() throws Exception{ SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); InputStream is = WeatherSaxTest.class.getClassLoader().getResourceAsStream("fuzhou_weather.xml"); WeatherSax handle = new WeatherSax(); parser.parse(is, handle); is.close(); return handle.getWeather(); }}
转载于:https://www.cnblogs.com/kaikailele/p/4178725.html
sax解析xml案例一相关推荐
- python 使用sax 解析xml 文件
这里不是说xml 的所以如果xml 不了解,可以百度大致看下即可, SAX知识了解 SAX (simple API for XML ) 有解析器和事件处理器 解析器负责读取XML文档,并向事件处理器 ...
- SAX解析XML文件
就目前来说,有三种方式可以解析XML文件:DOM.SAX.StAX.DOM将整个XML文件加载到内存中,并构建出节点树:应用程序可以通过遍历节点树的方式来解析XML文件中的各个节点.属性等信息:这种方 ...
- android xml解析demo,Android解析自定义xml文件--Sax解析xml文件,测试demo(方案二)...
转载请注明出处:http://blog.csdn.net/droyon/article/details/9346657 Sax解析xml 以下是测试Demo 运行程序类 public class Te ...
- python输出价目表-Python:使用基于事件驱动的SAX解析XML
SAX的特点: 是基于事件的 API 在一个比 DOM 低的级别上操作 为您提供比 DOM 更多的控制 几乎总是比 DOM 更有效率 但不幸的是,需要比 DOM 更多的工作 基于对象和基于事件的接口 ...
- SAX解析XML 详解
JAVA 解析 XML 通常有两种方式,DOM 和 SAX.DOM 虽然是 W3C 的标准,提供了标准的解析方式,但它的解析效率一直不尽如人意,因为使用DOM解析XML时,解析器读入整个文档并构建一个 ...
- 使用SAX解析XML文件
关于使用SAX解析XML文件也没什么要说明的,直接上代码吧. 关键如下: public class PersonHandler extends DefaultHandler {private Stri ...
- android 如何使用SAX解析XML
今天,简单讲讲android如何使用SAX解析XML . 昨天,我看代码时,看到了解析xml文档的代码,是使用SAX解析XML.但是我却不会使用SAX,于是在网上查找资料,最终解决了问题.这里记录一下 ...
- Java用SAX解析XML
2019独角兽企业重金招聘Python工程师标准>>> Myhandler package com.heli.xml.sax;/*** 用SAX解析XML的Handler*/ imp ...
- XML解析(一),SAX解析XML
转载自 XML解析(一),SAX解析XML 一.概述 SAX,全称Simple API for XML,是一种以事件驱动的XMl API,是XML解析的一种新的替代方法,解析XML常用的还有DOM ...
- android xml defaulthandler解析,sax解析xml文件的DefaultHandler处理类
一千年的时光,我无数次掀起岁月的帷幔,只为和你,在某一个平静如水的日子相遇,然后相识,倾情一生,缱绻一世,好美的散文,好吧,我情愿把这个"你"当作android:),使用sax解析 ...
最新文章
- WPF动画的属性被劫持
- index seek与index scan
- tp5.0分页样式调控
- 饿了么超级会员数量暴增,外卖市场“去泡沫化”的先声?
- 百度:土豪投机移动互联
- 代码编辑器揭露性格,你是哪一种?
- opengl 球纹理旋转源代码
- 穷人怎么慢慢打破阶层?做到这2点,活出最真实的样子,别表演!
- 如何让.Net线程支持超时后并自动销毁!
- yyds!Java 性能优化的 50 个细节(珍藏版)
- iis p访问php密码,访问IIS网站需要输入用户名密码(非匿名登录)问题汇总
- 利用HttpClient4,实现get,post 参数,post json,post file
- 如何录制电脑内部声音
- 拼多多和酷家乐面试经历总结(已拿offer)
- 百度地图车辆运动轨迹
- 嵌入式--LCD常用接口介绍
- 谷歌云| 5 个 GKE 功能可帮助您优化集群
- 计算机一级读取存储器,计算机一级MsOffice ​选择题
- Java(35):Java Base64编码和解码工具类
- 搜狗输入法词库php词库怎么用,中州韵输入法导入搜狗词库(示例代码)
热门文章
- python人脸比对算法_Python的人脸识别,欧式距离比对,机器训练,人脸采集,离线识别...
- 集合python_Python 集合
- squid代理与缓存(上)
- 130242014029-黄超强-实验一
- centos 打包RPM包 ntopng
- no talloc stackframe at ../source3/param/loadparm.c:4864, leaking memory
- 查询Mysql的数据架构信息研究
- 先装ubuntu后装windows时遇到的问题的解决办法
- 深入学习c++(虚函数遇到析构函数就退化了)
- 使用php+gmail 发送邮件