DOM4J 一个被广泛使用的技术,解析大文件时表现稳定,因此很多框架都是用这种方式解析。
1.准备一个XML文件
<?xml version="1.0" encoding="utf-8" ?>
<peopel>
<person personid="m001">
<name>Tony</name>
<address>beijing</address>
<tel>10086</tel>
<fax>3122321</fax>
<mail>3124432@qq.com</mail>
</person>
<person personid="m002">
<name>Tom</name>
<address>suzhou</address>
<tel>1892312325</tel>
<fax>5343</fax>
<mail>444444@qq.com</mail>
</person>
</peopel>
2.根据XML文件准备一个类
class Person{
private String personid;
private String name;
private String address;
private String tel;
private String fax;
private String mail;
public Person() {
}
public Person(String personid, String name, String address, String tel, String fax, String mail) {
this.personid = personid;
this.name = name;
this.address = address;
this.tel = tel;
this.fax = fax;
this.mail = mail;
}
@Override
public String toString() {
return "peopel{" +
"personid='" + personid + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
", tel='" + tel + '\'' +
", fax='" + fax + '\'' +
", mail='" + mail + '\'' +
'}';
}
public String getPersonid() {
return personid;
}
public void setPersonid(String personid) {
this.personid = personid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
3.设置Maven 配置
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.0.0</version>
</dependency>
4.上代码
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Mtest {
public static void main(String[] args) throws DocumentException {
SAXReader reader= new SAXReader();//创建解析器
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.xml");//读取文件流
Document dc= reader.read(is);//装载解析器
Element root= dc.getRootElement();//获取根节点
Iterator<Element> it=root.elementIterator();//利用迭代器接收根节点信息
//接收集合
List<Person> li=new ArrayList<>();
//--------循环迭代
//--------第一层迭代
while (it.hasNext()){
//---------准备接收对象
Person p=new Person();//注意接收对象在这里创建
Element e = it.next();
p.setPersonid(e.attributeValue("personid"));
System.out.println(e.attributeValue("personid"));
//进入第二层迭代
Iterator<Element> it2= e.elementIterator();
while (it2.hasNext()){
Element e2=it2.next();
String tag = e2.getName();
if ("name".equals(tag)){
System.out.println(e2.getText());
p.setName(e2.getText());
};
if ("address".equals(tag)){
p.setAddress(e2.getText());
};
if ("tel".equals(tag)){
p.setTel(e2.getText());
};
if ("fax".equals(tag)){
p.setFax(e2.getText());
};
if ("mail".equals(tag)){
p.setMail(e2.getText());
};
}
li.add(p);
}
//---------------验证
li.forEach((ps)->{
System.out.println(ps.toString());
});
}
}
效果
peopel{personid=‘m001’, name=‘Tony’, address=‘beijing’, tel=‘10086’, fax=‘3122321’, mail=‘3124432@qq.com’}
peopel{personid=‘m002’, name=‘Tom’, address=‘suzhou’, tel=‘1892312325’, fax=‘5343’, mail=‘444444@qq.com’}