使用SAX解析XML文件
source: http://blog.csdn.net/wangkuifeng0118/article/details/7305068
SAX是Simple API for XML的缩写,它并不是由W3C官方所提出的标准,虽然如此,使用SAX的还是不少,几乎所有的XML解析器都会支持它。
与DOM比较而言,SAX是一种轻量型的方法。我们知道,在处理DOM的时候,我们需要读入整个的XML文档,然后在内存中创建DOM树,生成DOM树上的每个Node对象。当文档比较小的时候,这不会造成什么问题,但是一旦文档大起来,处理DOM就会变得相当费时费力。特别是其对于内存的需求,也将是成倍的增长,以至于在某些应用中使用DOM是一件很不划算的事(比如在applet中)。这时候,一个较好的替代解决方法就是SAX。
SAX在概念上与DOM完全不同。它不同于DOM的文档驱动,它是事件驱动的,它并不需要读入整个文档,而文档的读入过程也就是SAX的解析过程。所谓事件驱动,是指一种基于回调(callback)机制的程序运行方法。
下面对这个xml文件使用sax解析:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <student id="1" group="1">
- <name>张三</name>
- <sex>男</sex>
- <age>18</age>
- <email>zhangsan@163.com</email>
- <birthday>1987-06-08</birthday>
- <memo>好学生</memo>
- </student>
- <student id="2" group="2">
- <name>李四</name>
- <sex>女</sex>
- <age>18</age>
- <email>lisi@163.com</email>
- <birthday>1987-06-08</birthday>
- <memo>好学生</memo>
- </student>
- <student id="3" group="3">
- <name>小王</name>
- <sex>男</sex>
- <age>18</age>
- <email>xiaowang@163.com</email>
- <birthday>1987-06-08</birthday>
- <memo>好学生</memo>
- </student>
- <student id="4" group="4">
- <name>小张</name>
- <sex>男</sex>
- <age>18</age>
- <email>xiaozhang@163.com</email>
- <birthday>1987-06-08</birthday>
- <memo>好学生</memo>
- </student>
- <student id="5" group="5">
- <name>小明</name>
- <sex>男</sex>
- <age>18</age>
- <email>xiaoming@163.com</email>
- <birthday>1987-06-08</birthday>
- <memo>好学生</memo>
- </student>
- </root>
xml对应的Javabean:
- public class Student {
- private int id;
- private int group;
- private String name;
- private String sex;
- private int age;
- private String email;
- private String memo;
- private String birthday;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getGroup() {
- return group;
- }
- public void setGroup(int group) {
- this.group = group;
- }
- 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 int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getMemo() {
- return memo;
- }
- public void setMemo(String memo) {
- this.memo = memo;
- }
- public String getBirthday() {
- return birthday;
- }
- public void setBirthday(String birthday) {
- this.birthday = birthday;
- }
- }
开始解析:
- import java.util.ArrayList;
- import java.util.List;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- /**
- * 功能描述:采用sax方式解析XML<br>
- *
- * @author sxyx2008
- *
- */
- public class SaxParseXml extends DefaultHandler{
- //存放遍历集合
- private List<Student> list;
- //构建Student对象
- private Student student;
- //用来存放每次遍历后的元素名称(节点名称)
- private String tagName;
- public List<Student> getList() {
- return list;
- }
- public void setList(List<Student> list) {
- this.list = list;
- }
- public Student getStudent() {
- return student;
- }
- public void setStudent(Student student) {
- this.student = student;
- }
- public String getTagName() {
- return tagName;
- }
- public void setTagName(String tagName) {
- this.tagName = tagName;
- }
- //只调用一次 初始化list集合
- @Override
- public void startDocument() throws SAXException {
- list=new ArrayList<Student>();
- }
- //调用多次 开始解析
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if(qName.equals("student")){
- student=new Student();
- //获取student节点上的id属性值
- student.setId(Integer.parseInt(attributes.getValue(0)));
- //获取student节点上的group属性值
- student.setGroup(Integer.parseInt(attributes.getValue(1)));
- }
- this.tagName=qName;
- }
- //调用多次
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- if(qName.equals("student")){
- this.list.add(this.student);
- }
- this.tagName=null;
- }
- //只调用一次
- @Override
- public void endDocument() throws SAXException {
- }
- //调用多次
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- if(this.tagName!=null){
- String date=new String(ch,start,length);
- if(this.tagName.equals("name")){
- this.student.setName(date);
- }
- else if(this.tagName.equals("sex")){
- this.student.setSex(date);
- }
- else if(this.tagName.equals("age")){
- this.student.setAge(Integer.parseInt(date));
- }
- else if(this.tagName.equals("email")){
- this.student.setEmail(date);
- }
- else if(this.tagName.equals("birthday")){
- this.student.setBirthday(date);
- }
- else if(this.tagName.equals("memo")){
- this.student.setMemo(date);
- }
- }
- }
- }
测试方法:
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParserFactory;
- import org.xml.sax.SAXException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
- public class Test {
- public static void main(String[] args) {
- SAXParser parser = null;
- try {
- //构建SAXParser
- parser = SAXParserFactory.newInstance().newSAXParser();
- //实例化 DefaultHandler对象
- SaxParseXml parseXml=new SaxParseXml();
- //加载资源文件 转化为一个输入流
- InputStream stream=SaxParseXml.class.getClassLoader().getResourceAsStream("student.xml");
- //调用parse()方法
- parser.parse(stream, parseXml);
- //遍历结果
- List<Student> list=parseXml.getList();
- for(Student student:list){
- System.out.println("id:"+student.getId()+"\tgroup:"+student.getGroup()+"\tname:"+student.getName()+"\tsex:"+student.getSex()+"\tage:"+student.getAge()+"\temail:"+student.getEmail()+"\tbirthday:"+student.getBirthday()+"\tmemo:"+student.getMemo());
- }
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
运行效果: