• 使用Dom4j解析XML文件


          与利用DOM、SAX、JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异、功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml。dom4j是一套开源的api。实际项目中,往往选择dom4j来作为解析xml的利器。

    所用jar包:dom4j-1.6.1.jar

     

    常用API

    class org.dom4j.io.SAXReader

    • read  提供多种读取xml文件的方式,返回一个Domcument对象

    interface org.dom4j.Document

    • iterator  使用此法获取node
    • getRootElement  获取根节点

    interface org.dom4j.Node

    • getName  获取node名字,例如获取根节点名称为bookstore
    • getNodeType  获取node类型常量值,例如获取到bookstore类型为1——Element
    • getNodeTypeName  获取node类型名称,例如获取到的bookstore类型名称为Element

    interface org.dom4j.Element

    • attributes  返回该元素的属性列表
    • attributeValue  根据传入的属性名获取属性值
    • elementIterator  返回包含子元素的迭代器
    • elements  返回包含子元素的列表

    interface org.dom4j.Attribute

    • getName  获取属性名
    • getValue  获取属性值

    interface org.dom4j.Text

    • getText  获取Text节点值

    interface org.dom4j.CDATA

    • getText  获取CDATA Section值

    interface org.dom4j.Comment

    • getText  获取注释 

    需要解析的xml文件:people.xml

    1 <people city="BeiJing">
    2     <student name="Jack" age="10">My name is Jack!</student>
    3     <student name="Lucy" age="11">My name is Lucy!</student>
    4     <student name="James" age="12"/>
    5     <teacher name="Jason" age="25">I am a teacher!</teacher>
    6     <teacher name="Michael" age="30">I am a teacher too!</teacher>
    7 </people>
    people.xml

    首先根据xml文档构建javabean:

     1 package bean;
     2 
     3 import java.util.List;
     4 
     5 public class People {
     6     private String city;
     7     private List<Student> student;
     8     private List<Teacher> teacher;
     9     
    10     public String getCity() {
    11         return city;
    12     }
    13     public void setCity(String city) {
    14         this.city = city;
    15     }
    16     public List<Student> getStudent() {
    17         return student;
    18     }
    19     public void setStudent(List<Student> student) {
    20         this.student = student;
    21     }
    22     public List<Teacher> getTeacher() {
    23         return teacher;
    24     }
    25     public void setTeacher(List<Teacher> teacher) {
    26         this.teacher = teacher;
    27     }
    28 }
    People.java
     1 package bean;
     2 
     3 public class Student {
     4     private String name;
     5     private int age;
     6     private String text;
     7     public String getName() {
     8         return name;
     9     }
    10     public void setName(String name) {
    11         this.name = name;
    12     }
    13     public int getAge() {
    14         return age;
    15     }
    16     public void setAge(int age) {
    17         this.age = age;
    18     }
    19     public String getResult() {
    20         return text;
    21     }
    22     public void setResult(String result) {
    23         this.text = result;
    24     }
    25     public Student(String name, int age, String text) {
    26         super();
    27         this.name = name;
    28         this.age = age;
    29         this.text = text;
    30     }
    31 }
    Student.java
     1 package bean;
     2 
     3 public class Teacher {
     4     private String name;
     5     private int age;
     6     private String text;
     7     public String getName() {
     8         return name;
     9     }
    10     public void setName(String name) {
    11         this.name = name;
    12     }
    13     public int getAge() {
    14         return age;
    15     }
    16     public void setAge(int age) {
    17         this.age = age;
    18     }
    19     public String getResult() {
    20         return text;
    21     }
    22     public void setResult(String result) {
    23         this.text = result;
    24     }
    25     public Teacher(String name, int age, String text) {
    26         super();
    27         this.name = name;
    28         this.age = age;
    29         this.text = text;
    30     }
    31 }
    Teacher.java
    解析代码如下:
     1 package xml;
     2 
     3 import java.io.File;
     4 import java.net.URISyntaxException;
     5 import java.util.ArrayList;
     6 import java.util.Iterator;
     7 import java.util.List;
     8 
     9 import org.dom4j.Document;
    10 import org.dom4j.DocumentException;
    11 import org.dom4j.Element;
    12 import org.dom4j.io.SAXReader;
    13 
    14 import bean.People;
    15 import bean.Student;
    16 import bean.Teacher;
    17 
    18 public class Dom4jParserXML {
    19     public static People parseXml(String xmlPath) {
    20         File xmlFile = new File(xmlPath);
    21         System.out.println(xmlFile.getPath());
    22         
    23         if (xmlFile.exists()) {
    24             SAXReader reader = new SAXReader();
    25             People people = new People();
    26             try {
    27                 // 读入文档流
    28                 Document document = reader.read(xmlFile);
    29                 // 获取根节点
    30                 Element root = document.getRootElement();
    31 
    32                 List<Student> students = new ArrayList<Student>();
    33                 List<Teacher> teachers = new ArrayList<Teacher>();
    34                 
    35                 people.setCity(root.attributeValue("city"));
    36                 // 解析student节点
    37                 for (Iterator<Element> iterator = root
    38                         .elementIterator("student"); iterator.hasNext();) {
    39                     Element eStudent = iterator.next();
    40                     String text = eStudent.getText();
    41                     
    42                     Student student = new Student(
    43                             eStudent.attributeValue("name"),
    44                             Integer.parseInt(eStudent.attributeValue("age")),text);
    45                     students.add(student);
    46                 }
    47                 // 解析teacher节点
    48                 for (Iterator<Element> iterator = root
    49                         .elementIterator("teacher"); iterator.hasNext();) {
    50                     Element eTeacher = iterator.next();
    51                     String text = eTeacher.getText();
    52                     
    53                     Teacher teacher = new Teacher(
    54                             eTeacher.attributeValue("name"),
    55                             Integer.parseInt(eTeacher.attributeValue("age")),text);
    56                     teachers.add(teacher);
    57                 }
    58                 people.setStudent(students);
    59                 people.setTeacher(teachers);
    60                 
    61                 System.out.println("Parse success!");
    62             } catch (DocumentException e) {
    63                 e.printStackTrace();
    64             }
    65             return people;
    66         } else {
    67             System.err.println("File is not exist!");
    68             return null;
    69         }
    70     }
    71 
    72     public static void main(String[] args) throws URISyntaxException {
    73         String xmlPath = "E://xml//people.xml";
    74         System.out.println("File full path is " + xmlPath);
    75         // 解析people.xml
    76         People p = parseXml(xmlPath);
    77 
    78         // 处理解析结果
    79         System.out.println("The city is : " + p.getCity());
    80         List<Student> sList = p.getStudent();
    81         List<Teacher> tList = p.getTeacher();
    82         for (Student s : sList) {
    83             System.out.println(s.getName() + "->" + s.getAge() + "->" + s.getResult());
    84         }
    85         for (Teacher t : tList) {
    86             System.out.println(t.getName() + "->" + t.getAge() + "->" + t.getResult());
    87         }
    88     }
    89 }

    运行结果:

    File full path is E://xml//people.xml
    E:\xml\people.xml
    Parse success!
    The city is : BeiJing
    Jack->10->My name is Jack!
    Lucy->11->My name is Lucy!
    James->12->
    Jason->25->I am a teacher!
    Michael->30->I am a teacher too!
    Result
  • 相关阅读:
    RHEL因为selinux设置失误,无法重启问题。(centos适用)
    Linux系统忘记管理员密码(CentOS、RHEL、Ubuntu)
    cobbler PXE 安装系统时出现的问题
    虚拟机ping 不通主机,主机可ping 虚拟机解决方法
    CentOS7系统更改网卡名为eth0
    PXE-cobbler 无人值守装机------续
    PXE-cobbler 无人值守装机
    DELL PowerEdge R410系统日志满报错
    matlab添加toolbox失败的解决办法
    vs2017+opencv3.4.0的配置方法
  • 原文地址:https://www.cnblogs.com/luomsg/p/4043136.html
Copyright © 2020-2023  润新知