记录读取XML文件方式二

Posted 转圈又是原点

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录读取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

以上是关于记录读取XML文件方式二的主要内容,如果未能解决你的问题,请参考以下文章

Flink读取HDFS中的xml(一)——读取XML的几种方式

记录一次springboot整合mybatis的xml方式

记录读取xml文件内容

java从xml读取数据更快还是从数据库里读取数据更快?

以编程方式替换片段

C# 调用动态链接库读取二代身份证信息