最近在 一些对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="数学"> 6 <class name="1612A" classroom="36306"> 7 <student> 8 <property name="name" value="张三"></property> 9 <property name="age" value="23"></property> 10 <property name="garden" value="男"></property> 11 </student> 12 <student> 13 <property name="name" value="李四"></property> 14 <property name="age" value="22"></property> 15 <property name="garden" value="女"></property> 16 </student> 17 <student> 18 <property name="name" value="王五"></property> 19 <property name="age" value="24"></property> 20 <property name="garden" value="男"></property> 21 </student> 22 </class> 23 </college> 24 25 <college name="英语"> 26 <class name="1612C" classroom="35108"> 27 <student> 28 <property name="name" value="赵六"></property> 29 <property name="age" value="21"></property> 30 <property name="garden" value="男"></property> 31 </student> 32 <student> 33 <property name="name" value="陈七"></property> 34 <property name="age" value="22"></property> 35 <property name="garden" value="男"></property> 36 </student> 37 <student> 38 <property name="name" value="郭八"></property> 39 <property name="age" value="25"></property> 40 <property name="garden" value="男"></property> 41 </student> 42 <student> 43 <property name="name" value="孙九"></property> 44 <property name="age" value="20"></property> 45 <property name="garden" value="女"></property> 46 </student> 47 </class> 48 </college> 49 50 </school>
Junit 测试如下:
1 public class Dom4jTest { 2 3 @Test 4 public void test() throws Exception{ 5
6 7 File xmlFile = new File("文件位置"); 8 // 调用工具类返回学生集合 9 List<Student> studentList = XMLUtil.fileTransferList(xmlFile); 10 11 for (Student student : studentList) { 12 13 System.out.println("-------------------------"); 14 System.out.println("姓名:"+student.getName()); 15 System.out.println("年龄:"+student.getAge()); 16 System.out.println("性别:"+student.getGarden()); 17 18 } 19 20 } 21 22 }
工具类如下:
1 public class XMLUtil { 2 3 public static List<Student> fileTransferList(File file) throws DocumentException{ 4 5 // 返回值:学生信息集合 6 List<Student> studentList=new ArrayList<Student>(); 7 8 // 创建saxReader对象 9 SAXReader reader = new SAXReader(); 10 11 // 通过read方法读取一个文件 转换成Document对象 12 Document document = reader.read(file); 13 14 //获取根节点元素对象 15 Element root = document.getRootElement(); 16 17 // 获取学院节点集合 18 List<Element> collegeElements = root.elements(); 19 20 //已知属性名情况下 21 for (Element college : collegeElements) { 22 23 List<Student> collegeStudentList = getStudentListFromCollegeElement(college); 24 25 studentList.addAll(collegeStudentList); 26 } 27 28 return studentList; 29 } 30 31 private static List<Student> getStudentListFromCollegeElement(Element collegeElement){ 32 33 // 返回值:学生信息集合 34 List<Student> studentList = new ArrayList<Student>(); 35 36 List<Element> classElements = collegeElement.elements(); 37 38 for (Element classElement : classElements) { 39 40 List<Student> classStudentList = getStudentListFromClassElement(classElement); 41 42 studentList.addAll(classStudentList); 43 } 44 45 return studentList; 46 47 } 48 49 private static List<Student> getStudentListFromClassElement(Element classElement){ 50 51 // 返回值:学生信息集合 52 List<Student> studentList = new ArrayList<Student>(); 53 54 List<Element> studentElements = classElement.elements(); 55 56 for (Element student : studentElements) { 57 58 List<Element> propertyElements = student.elements(); 59 60 Student student2 = studentElementTransferStudentEntity(propertyElements); 61 62 studentList.add(student2); 63 } 64 65 return studentList; 66 } 67 68 private static Student studentElementTransferStudentEntity(List<Element> propertyElements){ 69 70 Student stu = new Student(); 71 72 for (Element property : propertyElements) { 73 74 String name = property.attributeValue("name"); 75 String value = 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 }
最后呢,当然是显示结果了~~
结果如下:
到此结束了,以后也许有有其他解析的方法,也希望各位同道一块学习~~