Dom4J的XML解析案例

package com.czie.d1_dom4j;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * FileName: Dom4JTest2
 * Author:   lps
 * Date:     2022/4/28 20:10
 * Sign:刘品水 Q:1944900433
 */
public class Dom4JTest2 {

        @Test
        public void parseToList() throws Exception{
            //解析XML中书库 成为一个list集合
            //1、导入框架
            //2、创建saxReader对象
            SAXReader saxReader = new SAXReader();
            //3.加载xml文件成为文档对象Document对象
            Document document = saxReader.read(Dom4JTest2.class.getResourceAsStream("/Contacts.xml"));
            //4.拿到根元素
            Element root = document.getRootElement();
            //5.提取contact子元素
            List<Element> contactEles = root.elements("contact");
            //6.准备ArrayList集合封装联系人信息
            List<Contact> contacts=new ArrayList<>();
            //7.遍历Contact子元素
            for (Element contactEle : contactEles) {
                //8.每个子元素需要一个联系人对象
                Contact contact=new Contact();
                contact.setId(Integer.valueOf(contactEle.attributeValue("id")));
                contact.setVip(Boolean.valueOf(contactEle.attributeValue("vip")));
                contact.setName(contactEle.elementTextTrim("name"));
                contact.setGender(contactEle.elementTextTrim("gender").charAt(0));
                contact.setEmail(contactEle.elementTextTrim("email"));
                //9.把联系人对象数据加入到list集合
                contacts.add(contact);
            }

            //10.遍历list集合
            for (Contact contact : contacts) {
                System.out.println(contact);
            }



        }

}
package com.czie.d1_dom4j;

/**
 * FileName: Contact
 * Author:   lps
 * Date:     2022/4/28 20:11
 * Sign:刘品水 Q:1944900433
 * <contact id="1" vip="true">
 *         <name>   潘金莲  </name>
 *         <gender>女</gender>
 *         <email>panpan@qq.com</email>
 *     </contact>
 */
public class Contact {
    private String name;
    private int id;
    private boolean vip;
    private char gender;
    private String email;

    @Override
    public String toString() {
        return "Contact{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", vip=" + vip +
                ", gender=" + gender +
                ", email='" + email + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public boolean isVip() {
        return vip;
    }

    public void setVip(boolean vip) {
        this.vip = vip;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Contact() {
    }

    public Contact(String name, int id, boolean vip, char gender, String email) {
        this.name = name;
        this.id = id;
        this.vip = vip;
        this.gender = gender;
        this.email = email;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<contactList>
    <contact id="1" vip="true">
        <name>   潘金莲  </name>
        <gender></gender>
        <email>panpan@qq.com</email>
    </contact>
    <contact id="2" vip="false">
        <name>武松</name>
        <gender></gender>
        <email>wusong@itcast.cn</email>
    </contact>
    <contact id="3" vip="false">
        <name>武大狼</name>
        <gender></gender>
        <email>wuda@itcast.cn</email>
    </contact>
    <user>
    </user>
</contactList>

 

posted @ 2022-04-28 22:14  刘品水  阅读(53)  评论(0编辑  收藏  举报