记录读取XML文件方式二

记录:使用SAX方式读取,在速度上要优于DOM

 1 public class SchoolSax extends DefaultHandler {
 2     /**
 3      * 接收XML中所有有用的数据
 4      */
 5     private List<School> schoolList;
 6     /**
 7      * 临时变量
 8      */
 9     private School school;
10     private SClass sClass;
11     private Student student;
12 
13     public List<School> getSchoolList() {
14         return schoolList;
15     }
16 
17 
18     /**
19      * 文档接受文档开始的通知--此事件用于对象的初始化
20      */
21     @Override
22     public void startDocument() throws SAXException {
23         schoolList = new ArrayList<School>();
24 
25     }
26 
27     /**
28      * 文档接受结束
29      */
30     @Override
31     public void endDocument() throws SAXException {
32 
33     }
34 
35     @Override
36     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
37         if (qName.equals(SchoolTag.SCHOOL)) {
38             String schoolId = attributes.getValue(SchoolTag.ID);
39             String schoolName = attributes.getValue(SchoolTag.NAME);
40             school = new School(schoolId, schoolName);
41         } else if (qName.equals(SchoolTag.CLASS)) {
42             String sClassId = attributes.getValue(SchoolTag.ID);
43             String sClassName = attributes.getValue(SchoolTag.NAME);
44             sClass = new SClass(sClassId, sClassName, school);
45         } else if (qName.equals(SchoolTag.STUDENT)) {
46             String studentId = attributes.getValue(SchoolTag.ID);
47             String studentName = attributes.getValue(SchoolTag.NAME);
48             student = new Student(studentId, studentName, sClass);
49         } else {
50             System.out.println("其他的Tag:" + qName);
51         }
52     }
53 
54     @Override
55     public void endElement(String uri, String localName, String qName) throws SAXException {
56         if (qName.equals(SchoolTag.SCHOOL)) {
57             this.schoolList.add(school);
58         } else if (qName.equals(SchoolTag.CLASS)) {
59             this.school.getsClassList().add(sClass);
60         } else if (qName.equals(SchoolTag.STUDENT)) {
61             this.sClass.getStudents().add(student);
62         }
63     }
64 
65     @Override
66     public void characters(char[] ch, int start, int length) throws SAXException {
67 
68     }
69 }

Test:

 1 public class TestSax {
 2 
 3     public static void test(File file) throws Exception {
 4         SAXParserFactory factory = SAXParserFactory.newInstance();
 5         try {
 6             SAXParser saxParser = factory.newSAXParser();
 7             SchoolSax sax = new SchoolSax();
 8             //事件注册(允许多个接受者)
 9             saxParser.parse(file, sax);
10             List<School> schoolList = sax.getSchoolList();
11             for (School sch : schoolList) {
12                 System.out.println(sch.getId() + "--" + sch.getName());
13                 List<SClass> classList = sch.getsClassList();
14                 for (SClass sclass : classList) {
15                     System.out.println(sclass.getId() + "--" + sclass.getName());
16                     List<Student> studentList = sclass.getStudents();
17                     for (Student stu : studentList) {
18                         System.out.println(stu.getId() + "--" + stu.getName());
19                     }
20                 }
21             }
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25     }
26 
27 
28     public static void main(String[] args) {
29         try {
30             String path = TestSax.class.getResource("/").toURI().getPath();
31             String pathFileName = path + "\\School.xml";
32             TestSax.test(new File(pathFileName));
33         } catch (Exception e) {
34             e.printStackTrace();
35         }
36     }
37 }

同DOM相比,处理同一个XML文档,SAX用时:1s 459ms,DOM用时:4s 621ms

posted @ 2017-10-17 10:08  转圈又是原点  阅读(230)  评论(0编辑  收藏  举报