zoukankan      html  css  js  c++  java
  • dom4j解析xml

    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

  • 相关阅读:
    Matlab绘图基础——colormap在数字图像处理及三维图形展示上的应用(分层设色)
    [参考]C的scanf 和 C++的fscanf 的用法
    [转][修]C清空输入缓冲区
    在Windows下使用Navicat连接Linux下的MySql
    管理账号密码的工具-KeePass使用方法
    [转][修]利用matlab绘制地图上的点、线、面
    (Matlab)GPU计算所需的配置
    Matlab绘图基础——给图像配文字说明(text对象)
    Matlab绘图基础——图形修饰处理(入门)
    Matlab绘图基础——绘制向量图,二维三维(绘制参数曲线图)
  • 原文地址:https://www.cnblogs.com/lixud/p/6137960.html
Copyright © 2011-2022 走看看