与DOM比较而言,SAX是一种轻量型的方法。我们知道,在处理DOM的时候,我们需要读入整个的XML文档,然后在内存中创建DOM树,生成DOM树上的每个Node对象。当文档比较小的时候,这不会造成什么问题,但是一旦文档大起来,处理DOM就会变得相当费时费力。特别是其对于内存的需求,也将是成倍的增长,以至于在某些应用中使用DOM是一件很不划算的事(比如在applet中)。这时候,一个较好的替代解决方法就是SAX。
SAX在概念上与DOM完全不同。它不同于DOM的文档驱动,它是事件驱动的,它并不需要读入整个文档,而文档的读入过程也就是SAX的解析过程。所谓事件驱动,是指一种基于回调(callback)机制的程序运行方法。
下面对这个xml文件使用sax解析:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <root>
- studentid="1"group="1">
- name>张三</sex>男age>18email>zhangsan@163.combirthday>1987-06-08memo>好学生studentstudentid="2"group="2">李四>女>lisi@163.comstudentid="3"group="3">小王>xiaowang@163.comstudentid="4"group="4">小张>xiaozhang@163.comstudentid="5"group="5">小明>xiaoming@163.com>
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 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(); } } }
运行效果: