java中使dom4j解析XML文件

官网下载Dom4j地方:https://dom4j.github.io/ 
注意:使用Dom4j开发,需下载dom4j相应的jar文件

1.导入maven依赖

题目:后台利用dom4j解析student.xml文件,并赶回List<Student>集合

<!– parser xml file –>
manbetx手机网页版,<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.0.1</version>
</dependency>

得分析的XML:

2.创建xml文件

<?xml version="1.0" encoding="UTF-8"?>
<classes>
    <class name="ST01">
        <student id="10001">user1</student>
        <student id="10002">user2</student>
        <student id="10003">user3</student>
    </class>
    <class name="ST02">
        <student id="10004">user4</student>
        <student id="10005">user5</student>
        <student id="10006">user6</student>
    </class>
    <class name="ST03">
        <student id="10007">user7</student>
        <student id="10008">user8</student>
        <student id="10009">user9</student>
    </class>
    <class name="ST04">
        <student id="10010">user10</student>
        <student id="10011">user12</student>
        <student id="10012">user13</student>
    </class>
    <class name="ST05">
        <student id="10013">user14</student>
        <student id="10014">user15</student>
        <student id="10015">user16</student>
    </class>
</classes>

<?xml version=”1.0″ encoding=”UTF-8″?>
<users>
<user>
<userName>user1</userName>
<password>123</password>
<systemId>AFDWERDF</systemId>
</user>
<user>
<userName>user2</userName>
<password>456</password>
<systemId>AFDWERDFSSSSSSSSSS</systemId>
</user>
</users>

java解析xml代码:

3.加载跟用

/**
 * @author hh
 */
public class XmlUtil {
    /**
     * 读取xml学生列表信息
     * @return
     */
    public List<Student> listStudent(){
        //创建SAXReader对象
        SAXReader reader=new SAXReader();
        Document document = null;
        try {
            //通过read方法读取一个文件 转换成Document对象
            document = reader.read(new File("/Users/mac/Documents/students.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //获取根节点元素对象
        Element node = document.getRootElement();
        return elementMethod(node);
    }

    /**
     * 获取节点中的信息
     * @param node
     * @return
     */
    private List<Student> elementMethod(Element node){
        List<Student> list = new ArrayList<Student>();
        //获取所有class 节点
        List<Element> elementClass = node.elements("class");
        for (Element aClass : elementClass) {
            //获取所有student节点
            List<Element> elelmentStu=aClass.elements("student");
            for (Element element : elelmentStu) {
                //创建学生对象
                Student stu = new Student();
                //class节点里name的值
                stu.setSclazz(aClass.attribute("name").getValue());
                //student节点里ID的值
                stu.setSid(element.attribute("id").getValue());
                //student节点里的String值
                stu.setSname(element.getStringValue());

                list.add(stu);
            }
        }

        return list;
    }
}

String path = “src/main/resources/conf/syncEmployInfo.xml”;

实体类:

SAXReader reader = new SAXReader();
reader.setEncoding(“utf-8”);
Document document=null;;
try {
document = reader.read(new File(path));
Element root = document.getRootElement();//得到xml跟标签
List<Element> list = root.elements(“user”);

manbetx手机网页版 1manbetx手机网页版 2

List<EmployeeSynReqmodel> models = new ArrayList<>();
for (Element element : list) {
EmployeeSynReqmodel model = new EmployeeSynReqmodel();
String userName = element.element(“userName”).getStringValue();
String password = element.element(“password”).getStringValue();
String systemId = element.element(“systemId”).getStringValue();

/**
 * @author hh
 */
public class Student {
    private String sname;
    private String sid;
    private String sclazz;


    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getSclazz() {
        return sclazz;
    }

    public void setSclazz(String sclazz) {
        this.sclazz = sclazz;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sname='" + sname + '\'' +
                ", sid='" + sid + '\'' +
                ", sclazz='" + sclazz + '\'' +
                '}';
    }
}

model.setUserName(userName);
model.setPassword(password);
model.setSystemId(systemId);
models.add(model);
}

View Code

 

@Test 代码:

manbetx手机网页版 3manbetx手机网页版 4

public class StudentDaoTest {
    @Test
    public void testlistStudent(){
        List<Student> list = new XmlUtil().listStudent();
        for (Student student : list) {
            System.out.println(student);
        }
    }
}

View Code

 

运转结果:

manbetx手机网页版 5

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图