数据量大的xml建议使用SAX解析提高解析速度。SAX是Simple API for XML的缩写,它是事件驱动的,它并不需要读入整个文档,而文档的读入过程也就是SAX的解析过程。所谓事件驱动,是指一种基于回调(callback)机制的程序运行方法。
解析过程:
(1)创建SAX解析器
SAXParserFactory factory=SAXParserFactory.newInstance();
(2)实现解析DefaultHandler
主要实现其中的5个方法:
Void startDocument(), void endDocument(), void startElement(String uri,String localName,string qName,Attributes attr).
Void endElement(String uri,String localName,string qName),void characters(char[] ch,int start,int lenth)
下面对这个xml文件使用sax解析:
<?xml version="1.0" encoding="UTF-8"?> <root> <student id="1" group="1"> <name>张三</name> <sex>男</sex> <age>18</age> <email>[email protected]</email> <birthday>1987-06-08</birthday> <memo>好学生</memo> </student> <student id="2" group="2"> <name>李四</name> <sex>女</sex> <age>18</age> <email>[email protected]</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; //省略get/set方法...... }
开始解析:
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 smn * */ 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(student); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/11720.html