最近在 一些对xml文件的操作,下面简单写一个dom4j解析xml文件并将其封装到一个javabean中的例子,只是具有针对性的,不是通用的,仅供参考哦~~
首先说:dom4j是一个java的XML api,性能优异、功能强大、易于使用。使用dom4j对xml文件进行解析,并完成对文件的封装。
接下来,主要使用到的是dom4j中的SAXReader类,在这里我的流程是传入一个xml文件,调用写好的的工具类,完成对xml文件的解析。
xml文件如下:
1 xml version="1.0" encoding="UTF-8"?>
2
3 <school>
4
5 <college name&#61;"数学">
6 <class name&#61;"1612A" classroom&#61;"36306">
7 <student>
8 <property name&#61;"name" value&#61;"张三">property>
9 <property name&#61;"age" value&#61;"23">property>
10 <property name&#61;"garden" value&#61;"男">property>
11 student>
12 <student>
13 <property name&#61;"name" value&#61;"李四">property>
14 <property name&#61;"age" value&#61;"22">property>
15 <property name&#61;"garden" value&#61;"女">property>
16 student>
17 <student>
18 <property name&#61;"name" value&#61;"王五">property>
19 <property name&#61;"age" value&#61;"24">property>
20 <property name&#61;"garden" value&#61;"男">property>
21 student>
22 class>
23 college>
24
25 <college name&#61;"英语">
26 <class name&#61;"1612C" classroom&#61;"35108">
27 <student>
28 <property name&#61;"name" value&#61;"赵六">property>
29 <property name&#61;"age" value&#61;"21">property>
30 <property name&#61;"garden" value&#61;"男">property>
31 student>
32 <student>
33 <property name&#61;"name" value&#61;"陈七">property>
34 <property name&#61;"age" value&#61;"22">property>
35 <property name&#61;"garden" value&#61;"男">property>
36 student>
37 <student>
38 <property name&#61;"name" value&#61;"郭八">property>
39 <property name&#61;"age" value&#61;"25">property>
40 <property name&#61;"garden" value&#61;"男">property>
41 student>
42 <student>
43 <property name&#61;"name" value&#61;"孙九">property>
44 <property name&#61;"age" value&#61;"20">property>
45 <property name&#61;"garden" value&#61;"女">property>
46 student>
47 class>
48 college>
49
50 school>
Junit 测试如下&#xff1a;
1 public class Dom4jTest {
2
3 &#64;Test
4 public void test() throws Exception{
5
6
7 File xmlFile &#61; new File("文件位置");
8 // 调用工具类返回学生集合
9 List
10
11 for (Student student : studentList) {
12
13 System.out.println("-------------------------");
14 System.out.println("姓名&#xff1a;"&#43;student.getName());
15 System.out.println("年龄&#xff1a;"&#43;student.getAge());
16 System.out.println("性别&#xff1a;"&#43;student.getGarden());
17
18 }
19
20 }
21
22 }
工具类如下&#xff1a;
1 public class XMLUtil {
2
3 public static List
4
5 // 返回值&#xff1a;学生信息集合
6 List
7
8 // 创建saxReader对象
9 SAXReader reader &#61; new SAXReader();
10
11 // 通过read方法读取一个文件 转换成Document对象
12 Document document &#61; reader.read(file);
13
14 //获取根节点元素对象
15 Element root &#61; document.getRootElement();
16
17 // 获取学院节点集合
18 List
19
20 //已知属性名情况下
21 for (Element college : collegeElements) {
22
23 List
24
25 studentList.addAll(collegeStudentList);
26 }
27
28 return studentList;
29 }
30
31 private static List
32
33 // 返回值&#xff1a;学生信息集合
34 List
35
36 List
37
38 for (Element classElement : classElements) {
39
40 List
41
42 studentList.addAll(classStudentList);
43 }
44
45 return studentList;
46
47 }
48
49 private static List
50
51 // 返回值&#xff1a;学生信息集合
52 List
53
54 List
55
56 for (Element student : studentElements) {
57
58 List
59
60 Student student2 &#61; studentElementTransferStudentEntity(propertyElements);
61
62 studentList.add(student2);
63 }
64
65 return studentList;
66 }
67
68 private static Student studentElementTransferStudentEntity(List
69
70 Student stu &#61; new Student();
71
72 for (Element property : propertyElements) {
73
74 String name &#61; property.attributeValue("name");
75 String value &#61; property.attributeValue("value");
76
77 if("name".equals(name)){
78 stu.setName(value);
79 }
80 if("age".equals(name)){
81 stu.setAge(value);
82 }
83 if("garden".equals(name)){
84 stu.setGarden(value);
85 }
86 }
87
88 return stu;
89 }
90
91 }
最后呢&#xff0c;当然是显示结果了~~
结果如下&#xff1a;
到此结束了&#xff0c;以后也许有有其他解析的方法&#xff0c;也希望各位同道一块学习~~