Java超简单解析XML文件

Java详细解析XML文件

Java解析XML文件一般都是为了从外部配置Java程序,从而满足开闭原则。

我们采用DOM4J的方式解析XML,首先准备DOM4J的架包,可以在以下网址下载最新版DOM4J

https://dom4j.github.io

下面我们准备一下mvc.xml文件

<mokerson>
    <!--需要拦截的动作名称-->
    <action name="login" class="com.mokerson.servlet.Login">
        <!--拦截后当返回success的时候访问index.jsp-->
        <result name="success">index.jsp</result>
        <!--同理,拦截后当返回error的时候访问login.jsp-->
        <result name="error">login.jsp</result>
    </action>

    <action name="register" class="com.mokerson.servlet.Register">
        <result name="success">register.jsp</result>
        <result name="error">login.jsp</result>
    </action>
</mokerson>

然后在Java程序中解析XML

package com.mokerson.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author TanGuozheng
 * Create Time 2019/9/6 15:21
 */

public class ActionOperation {

    public static Map<String, Object> dataMap = new HashMap<>();

    public void readyData(String fileName) {
        SAXReader reader = new SAXReader();
        String name = null;
        ActionInfo actionInfo = null;
        try {
            // 通过reader对象的read方法加载fileName文件,获取document对象。
            Document document = reader.read(new File(fileName));

            // 通过document对象获取根节点,也就是在我们的xml中找到最顶层的节点
            Element root = document.getRootElement();

            // 通过root对象获取迭代器
            Iterator iterator = root.elementIterator();
            while (iterator.hasNext()) {
                // 创建对象,存储数据
                actionInfo = new ActionInfo();
                // 获取子节点
                Element firstElement = (Element) iterator.next();

                // 获取节点的属性名为name的值
                name = firstElement.attributeValue("name");
                actionInfo.setName(name);
                // 获取节点的属性名为class的值
                actionInfo.setClasses(firstElement.attributeValue("class"));

                // 获取此节点下子节点的遍历
                Iterator iterator1 = firstElement.elementIterator();

                Map<String, String> map = new HashMap<>(0);
                while (iterator1.hasNext()) {
                    // 获取节点
                    Element secondElement = (Element) iterator1.next();
                    // 获取节点的属性名为name的值
                    String key = secondElement.attributeValue("name");

                    // 获取节点中的内容,也就是标记对中间的内容
                    // 例如:<a>点击</a> 那么就是获取“点击”;
                    String value = secondElement.getText();
                    map.put(key,value);
                }
                actionInfo.setMap(map);
            }
            dataMap.put(name,actionInfo);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new ActionOperation().readyData("mvc.xml");
        System.out.println();
    }
}

注意mvc.xml的位置,直接放在项目下面就好了,就是src目录下

发布了28 篇原创文章 · 获赞 2 · 访问量 941

猜你喜欢

转载自blog.csdn.net/TanGuozheng_Java/article/details/100585481
今日推荐