最近在 一些对xml文件的操作,下面简单写一个dom4j解析xml文件并将其封装到一个javabean中的例子,只是具有针对性的,不是通用的,仅供参考哦~~
首先说:dom4j是一个java的XML api,性能优异、功能强大、易于使用。使用dom4j对xml文件进行解析,并完成对文件的封装。
接下来,主要使用到的是dom4j中的SAXReader类,在这里我的流程是传入一个xml文件,调用写好的的工具类,完成对xml文件的解析。
xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <school> <college name="数学">
<class name="1612A" classroom="36306">
<student>
<property name="name" value="张三"></property>
<property name="age" value="23"></property>
<property name="garden" value="男"></property>
</student>
<student>
<property name="name" value="李四"></property>
<property name="age" value="22"></property>
<property name="garden" value="女"></property>
</student>
<student>
<property name="name" value="王五"></property>
<property name="age" value="24"></property>
<property name="garden" value="男"></property>
</student>
</class>
</college> <college name="英语">
<class name="1612C" classroom="35108">
<student>
<property name="name" value="赵六"></property>
<property name="age" value="21"></property>
<property name="garden" value="男"></property>
</student>
<student>
<property name="name" value="陈七"></property>
<property name="age" value="22"></property>
<property name="garden" value="男"></property>
</student>
<student>
<property name="name" value="郭八"></property>
<property name="age" value="25"></property>
<property name="garden" value="男"></property>
</student>
<student>
<property name="name" value="孙九"></property>
<property name="age" value="20"></property>
<property name="garden" value="女"></property>
</student>
</class>
</college> </school>
Junit 测试如下:
public class Dom4jTest { @Test
public void test() throws Exception{
6
File xmlFile = new File("文件位置");
// 调用工具类返回学生集合
List<Student> studentList = XMLUtil.fileTransferList(xmlFile); for (Student student : studentList) { System.out.println("-------------------------");
System.out.println("姓名:"+student.getName());
System.out.println("年龄:"+student.getAge());
System.out.println("性别:"+student.getGarden()); } } }
工具类如下:
public class XMLUtil { public static List<Student> fileTransferList(File file) throws DocumentException{ // 返回值:学生信息集合
List<Student> studentList=new ArrayList<Student>(); // 创建saxReader对象
SAXReader reader = new SAXReader(); // 通过read方法读取一个文件 转换成Document对象
Document document = reader.read(file); //获取根节点元素对象
Element root = document.getRootElement(); // 获取学院节点集合
List<Element> collegeElements = root.elements(); //已知属性名情况下
for (Element college : collegeElements) { List<Student> collegeStudentList = getStudentListFromCollegeElement(college); studentList.addAll(collegeStudentList);
} return studentList;
} private static List<Student> getStudentListFromCollegeElement(Element collegeElement){ // 返回值:学生信息集合
List<Student> studentList = new ArrayList<Student>(); List<Element> classElements = collegeElement.elements(); for (Element classElement : classElements) { List<Student> classStudentList = getStudentListFromClassElement(classElement); studentList.addAll(classStudentList);
} return studentList; } private static List<Student> getStudentListFromClassElement(Element classElement){ // 返回值:学生信息集合
List<Student> studentList = new ArrayList<Student>(); List<Element> studentElements = classElement.elements(); for (Element student : studentElements) { List<Element> propertyElements = student.elements(); Student student2 = studentElementTransferStudentEntity(propertyElements); studentList.add(student2);
} return studentList;
} private static Student studentElementTransferStudentEntity(List<Element> propertyElements){ Student stu = new Student(); for (Element property : propertyElements) { String name = property.attributeValue("name");
String value = property.attributeValue("value"); if("name".equals(name)){
stu.setName(value);
}
if("age".equals(name)){
stu.setAge(value);
}
if("garden".equals(name)){
stu.setGarden(value);
}
} return stu;
} }
最后呢,当然是显示结果了~~
结果如下:
到此结束了,以后也许有有其他解析的方法,也希望各位同道一块学习~~