记录读取xml文件内容

Posted 转圈又是原点

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录读取xml文件内容相关的知识,希望对你有一定的参考价值。

记录下使用document类去读取xml文件内容

xml内容:

 1 <?xml version="1.0" encoding = "UTF-8"?>
 2 <schools>
 3     <school id="1" name="school1">
 4         <class id="11" name="class11">
 5             <student id="111" name="student111"/>
 6             <student id="112" name="student112"/>
 7             <student id="113" name="student113"/>
 8         </class>
 9         <class id="12" name="class12">
10             <student id="121" name="student121"/>
11             <student id="122" name="student122"/>
12         </class>
13         <class id="13" name="class13">
14             <student id="131" name="student131"/>
15             <student id="132" name="student132"/>
16         </class>
17     </school>
18     <school id="2" name="school2">
19         <class id="21" name="class21">
20             <student id="211" name="student211"/>
21         </class>
22     </school>
23 </schools>

使用getElementsByTagName读取文件

定义学校类: 

 1 public class School {
 2 
 3     private String name;
 4     private String id;
 5     private List<SClass> sClassList;
 6 
 7     public School(String name, String id) {
 8         this.name = name;
 9         this.id = id;
10         sClassList = new ArrayList<SClass>();
11     }
12 
13     public String getName() {
14         return name;
15     }
16 
17     public String getId() {
18         return id;
19     }
20 
21     public List<SClass> getsClassList() {
22         return sClassList;
23     }
24 }

 

定义班级类:

 1 public class SClass {
 2     private String id;
 3     private String name;
 4     private School school;
 5     private List<Student> students;
 6 
 7 
 8     public SClass(String id, String name, School school) {
 9         this.id = id;
10         this.name = name;
11         this.school = school;
12         students = new ArrayList<Student>();
13         school.getsClassList().add(this);
14     }
15 
16     public String getId() {
17         return id;
18     }
19 
20 
21     public String getName() {
22         return name;
23     }
24 
25 
26     public School getSchool() {
27         return school;
28     }
29 
30 
31     public List<Student> getStudents() {
32         return students;
33     }
34 }
 

定义学生类:

 1 public class Student {
 2     private String id;
 3     private String name;
 4     private SClass sClass;
 5 
 6 
 7     public Student(String id, String name, SClass sClass) {
 8         this.id = id;
 9         this.name = name;
10         this.sClass = sClass;
11         sClass.getStudents().add(this);
12     }
13 
14     public String getId() {
15         return id;
16     }
17 
18     public String getName() {
19         return name;
20     }
21 
22     public SClass getsClass() {
23         return sClass;
24     }
25 }

定义读取xml类:

 1 public class SchoolDOM {
 2     private Document document;
 3 
 4     public SchoolDOM(File file) {
 5         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();     //DOM树的解析器
 6         try {
 7             DocumentBuilder documentBuilder = factory.newDocumentBuilder();
 8             document = documentBuilder.parse(file);
 9         } catch (Exception e) {
10             e.printStackTrace();
11         }
12     }
13 
14     /**
15      * parse school.xml,返回所有学校、班级、学生的信息
16      *
17      * @return
18      */
19     public List<School> parseSchoolXml() {
20         List<School> schools = null;
21         if (document != null) {
22             schools = new ArrayList<School>();
23             NodeList schoolList = document.getElementsByTagName(SchoolTag.SCHOOL);          //学校的节点
24             for (int i = 0; i < schoolList.getLength(); i++) {
25                 Node schoolNode = schoolList.item(i);
26                 if (schoolNode.getNodeType() == Node.ELEMENT_NODE) {
27                     Element schoolElement = (Element) schoolNode;               //把节点(node)强转成元素(element)
28                     String schoolID = schoolElement.getAttribute(SchoolTag.ID);
29                     String schoolName = schoolElement.getAttribute(SchoolTag.NAME);
30                     School school = new School(schoolID, schoolName);
31                     schools.add(school);
32                     NodeList classList = schoolElement.getChildNodes();                   //班级的节点
33                     for (int j = 0; j < classList.getLength(); j++) {
34                         Node classNode = classList.item(j);
35                         if (classNode.getNodeType() == Node.ELEMENT_NODE) {
36                             Element classElement = (Element) classNode;
37                             String classID = classElement.getAttribute(SchoolTag.ID);
38                             String className = classElement.getAttribute(SchoolTag.NAME);
39                             SClass sclass = new SClass(classID, className, school);
40                             NodeList studentList = classElement.getChildNodes();              //学生的节点
41                             for (int k = 0; k < studentList.getLength(); k++) {
42                                 Node studentNode = studentList.item(k);
43                                 if (studentNode.getNodeType() == Node.ELEMENT_NODE) {
44                                     Element studentElement = (Element) studentNode;
45                                     String studentID = studentElement.getAttribute(SchoolTag.ID);
46                                     String studentName = studentElement.getAttribute(SchoolTag.NAME);
47                                     Student student = new Student(studentID, studentName, sclass);
48                                 }
49                             }
50                         }
51                     }
52                 }
53             }
54         }
55 
56         return schools;
57     }
58 }

定义Tag常量

 1 public class SchoolTag {
 2 
 3     public static final String SCHOOL = "school";
 4     public static final String CLASS = "class";
 5     public static final String STUDENT = "student";
 6     public static final String ID = "id";
 7     public static final String NAME = "name";
 8 
 9 
10 }

测试方法

 1 public class TestDom {
 2     public static void test() {
 3         try {
 4             String path = TestDom.class.getResource("/").toURI().getPath();
 5             String pathFileName = path + "\\School.xml";
 6             File file = new File(pathFileName);
 7             SchoolDOM schoolDOM = new SchoolDOM(file);
 8             List<School> schools = schoolDOM.parseSchoolXml();
 9             for (School school : schools) {
10                 System.out.println(school.getId() + "---" + school.getName());
11                 List<SClass> classList = school.getsClassList();
12                 for (SClass sclass : classList) {
13                     System.out.println(sclass.getId() + "--" + sclass.getName());
14                     List<Student> students = sclass.getStudents();
15                     for (Student student : students) {
16                         StringBuilder stringBuilder = new StringBuilder();
17                         stringBuilder.append(student.getId());
18                         stringBuilder.append("--");
19                         stringBuilder.append(student.getName());
20                         System.out.println(stringBuilder);
21                     }
22                 }
23             }
24 
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28     }
29 
30 
31     public static void main(String[] args) {
32         TestDom.test();
33     }
34 }

输出结果

school1---1
11--class11
111--student111
112--student112
113--student113
12--class12
121--student121
122--student122
13--class13
131--student131
132--student132
school2---2
21--class21
211--student211

 

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

Excel2007中发现不可读取的内容

XML文件内容如何读取(C或C++)

请教一个问题,php如何读取mp3文件

Java里面怎么解析xml文件的内容

读取 zip 包中 xml 文件的内容

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途