dom4j解析项目xml

controller层

    @Test
                 public void parseXml() throws Exception {
                     // 1、创建SAXReader对象
                     final SAXReader reader = new SAXReader();

                     // 2、通过SAXReader对象读取xml文件生成Domcument文档对象
                     final Document document = reader.read(new File(PARSE_XML_PATH));

                     // 3、获取根节点对象
                     final Element root = document.getRootElement();

                     // 4、遍历
                     listNodes(root);
                 }

                 /**
                  * 遍历节点
                  * @param node
                  */
                 public void listNodes(Element node) {
                     // 获取节点的名称
                     final String nodeName = node.getName();

                     System.out.println(nodeName);

                     // 遍历属性节点
                    final List<Attribute> attributes = node.attributes();
                    for (Attribute attribute : attributes) {
                         System.out.println(attribute.getName() + "=" + attribute.getText()+"vvvvvvvvvvvvvvv");
                     }

                   // 获取当前节点的内容

                         System.out.println(node.getName() + "=" + node.getText()+"aaaaaaaaaaaaaaaaaaaaa");


                     //迭代该节点的所有子节点
                     final Iterator iterator = node.elementIterator();
                     while (iterator.hasNext()) {
                         Element el = (Element) iterator.next();
                         listNodes(el); // 使用递归
                     }
         }

firstxml.xml

<?xml version="1.0" ?>
<!-- 所在部门标签 -->
<department>
    <!-- 员工标签:属性number是工号 -->
    <employee number="001">
        <!-- 员工的个人信息 -->
        <name>小海</name>
        <sex></sex>
        <profession>java开发</profession>
        <hobby>足球</hobby>
    </employee>
    <employee number="002">
        <!-- 员工的个人信息 -->
        <name>婷菲</name>
        <sex></sex>
        <profession>C#开发</profession>
        <hobby>跑步</hobby>
    </employee>
</department>

XmlParam.java

package com.shy.pojo;


public class XmlParam {
    public static final String LABLE_EMPLOYEE = "employee"; // 员工标签
    public static final String LABLE_NAME = "name"; // 名称标签
    public static final String LABLE_SEX = "sex"; // 性别标签
    public static final String LABLE_PROFESSION = "profession"; // 职业标签
    public static final String LABLE_HOBBY = "hobby"; // 爱好标签

    private int number;
    private String name;
    private String sex;
    private String profession;
    private String hobby;

    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getProfession() {
        return profession;
    }
    public void setProfession(String profession) {
        this.profession = profession;
    }
    public String getHobby() {
        return hobby;
    }
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("number=").append(number).append("\n")
            .append("name=").append(name).append("\n")
            .append("sex=").append(sex).append("\n")
            .append("profession=").append(profession).append("\n")
            .append("hobby=").append(hobby).append("\n");
        return builder.toString();
    }
}

猜你喜欢

转载自blog.csdn.net/a520songhai/article/details/80877974