package com.itheima.d1_dom4j; /** * <contact id="1" vip="true"> * <name> 潘金莲 </name> * <gender>女</gender> * <email>panpan@itcast.cn</email> * </contact> */ public class Contact { private String name; private int id; private boolean vip; private char gender; private String 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; } 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; } @Override public String toString() { return "Contact{" + "name='" + name + '\'' + ", id=" + id + ", vip=" + vip + ", gender=" + gender + ", email='" + email + '\'' + '}'; } }
<?xml version="1.0" encoding="UTF-8"?> <contactList> <contact id="1" vip="true"> <name> 潘金莲 </name> <gender>女</gender> <email>panpan@itcast.cn</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>
package com.itheima.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; 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("/com\\itheima\\d1_dom4j\\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.elementText("email")); //9.把联系人对象数据加入到List集合 contacts.add(contact); } //10.遍历List集合 for (Contact contact : contacts) { System.out.println(contact); } } }