dom4j解析xml

眉间皱痕 提交于 2020-03-24 11:39:44
2016-12-06
 
//xml
 
<?xml version="1.0" encoding="UTF-8" ?>
- <school>
- <college name="孔子学院">
- <class name="123" classroom="36306">
- <student>
  <property name="name" value="航三" />
  <property name="age" value="23" />
  <property name="garden" value="" />
  </student>
- <student>
  <property name="name" value="李希" />
  <property name="age" value="22" />
  <property name="garden" value="" />
  </student>
- <student>
  <property name="name" value="王五" />
  <property name="age" value="24" />
  <property name="garden" value="" />
  </student>
  </class>
  </college>
- <college name="北大">
- <class name="456" classroom="35108">
- <student>
  <property name="name" value="赵柳" />
  <property name="age" value="21" />
  <property name="garden" value="" />
  </student>
- <student>
  <property name="name" value="刘琪" />
  <property name="age" value="22" />
  <property name="garden" value="" />
  </student>
- <student>
  <property name="name" value="周扒皮" />
  <property name="age" value="25" />
  <property name="garden" value="" />
  </student>
- <student>
  <property name="name" value="赵qq" />
  <property name="age" value="20" />
  <property name="garden" value="" />
  </student>
  </class>
  </college>
  </school>

 

/****************************************/

//测试类

import java.io.File;

import java.util.List;

import org.junit.Test;

import entity.Student;

public class Dom4jTest {

@Test
public void test() throws Exception{

// 创建saxReader对象
File xmlFile = new File("C:/Users/zb/Desktop/studentInfo.xml");

List<Student> studentList = XMLUtil.fileTransferList(xmlFile);

for (Student student : studentList) {
System.out.println(student.getName());
}

}

}

/***********************************************/

//工具类

package domFourj;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import entity.Student;


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;
}

}

/****************************/

//实现效果

姓名:航三
姓名:李希
姓名:王五
姓名:赵柳
姓名:刘琪
姓名:周扒皮
姓名:赵qq

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!