博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xml通过dom4j解析成java对象
阅读量:6958 次
发布时间:2019-06-27

本文共 5473 字,大约阅读时间需要 18 分钟。

要解析的xml

java代码解析前一定要先把jar包导入

在这里插入图片描述
接下来我来定义实体类
实体类根据上面的xml去定义。实体类定义的顺序一定是由里向外去定义的。
定义顺序 forward标签模型 -> actiong标签模型 -> config标签模型

forward标签模型

public class ForwardModal {    public ForwardModal() {        super();    }            public ForwardModal(String name, String path, boolean redirct) {        super();        this.name = name;        this.path = path;        this.redirct = redirct;    }    private String name;    private String path;    private boolean redirct;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPath() {        return path;    }    public void setPath(String path) {        this.path = path;    }    public boolean isRedirct() {        return redirct;    }    public void setRedirct(boolean redirct) {        this.redirct = redirct;    }    @Override    public String toString() {        return "ForwardModal [name=" + name + ", path=" + path + ", redirct=" + redirct + "]";    }}

action标签模型

/** * Action标签模型 * @author 20190313 * */public class ActionModal {    private String path;    private String type;    private Map
map = new HashMap<>(); public void addForwardModal(ForwardModal forwardModal) { //判断当前的name是不是有重复数据,如果没有则添加,有则跳过 if(!map.containsKey(forwardModal.getName())) { map.put(forwardModal.getName(), forwardModal); } } public ForwardModal getForwardModal(String key) { return map.get(key); } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "ActionModal [path=" + path + ", type=" + type + ", map=" + map + "]"; }}

config标签模型

public class ConfigModal {    private Map
map = new HashMap<>(); public void addActionModal(ActionModal actionModal) { if(!map.containsKey(actionModal.getType())) { map.put(actionModal.getType(), actionModal); } } public ActionModal getActionModal(String key) { return map.get(key); } @Override public String toString() { return "ConfigModal [map=" + map + "]"; }}

将xml解析成java对象的工厂类

记住解析的方式一定是从外往里。先解析 config节点 config节点里面添加action节点。 action节点中添加forward节点

/** * 配置文件转成java对象的工厂类 *  * @author 20190313 * */public class ConfigFactory {    private static final String DEFAUL_PATH = "/config.xml";    // 单利模式:只会创建一个对象    private static ConfigModal configModal = null;    /**     * 加载默认路径下的信息     *      * @return     */    public static ConfigModal createConfig() {        return createConfig(DEFAUL_PATH);    }    /**     * 加载指定目录下的信息     * 返回解析xml后等到的对象(ConfigModal)     * @param path     * @return     */    public static ConfigModal createConfig(String path) {        if (configModal == null) {            configModal = new ConfigModal();        }        // 开始Dom4j解析        SAXReader saxReader = new SAXReader();        try {            //生成指定路径对应的xml文档对象            Document document = saxReader.read(ConfigFactory.class.getResourceAsStream(path));            // 读取ActionModal对象到configModal中            createAction(document, configModal);        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                return configModal;    }        //这是将action节点解析成ActiongModal对象并放入ConfigModal    private static void createAction(Document document, ConfigModal configModal) {        List actionList = document.selectNodes("/config/action");        //读取当前action节点的属性 并对当前action对象的属性进行赋值        ActionModal actionModal= null;        for (Object object : actionList) {            actionModal = new ActionModal();            Element actionElement = (Element)object;            // 给当前的属性进行赋值            actionModal.setPath(actionElement.attributeValue("path"));            actionModal.setType(actionElement.attributeValue("type"));            //给当前的action添加ForWord的属性             createFrowrd(actionElement, actionModal);            configModal.addActionModal(actionModal);        }    }        //将frowrd节点解析成FrowrdModal并放入ActiongModal对象中    private static void createFrowrd(Element actionElement, ActionModal actionModal) {        List forwardList = actionElement.selectNodes("forward");        ForwardModal forwardModal = null;        for (Object object : forwardList) {            forwardModal = new ForwardModal();            Element forwardElement = (Element)object;            forwardModal.setName(forwardElement.attributeValue("name"));            forwardModal.setPath(forwardElement.attributeValue("path"));            forwardModal.setRedirct(Boolean.valueOf(forwardElement.attributeValue("redirect")));            actionModal.addForwardModal(forwardModal);        }    }}

测试一下

public static void main(String[] args) {        ConfigModal configModal = ConfigFactory.createConfig();        System.out.println(configModal.getActionModal("test.RegAction").getPath());        System.out.println(configModal.getActionModal("test.RegAction").getType());        System.out.println(configModal.getActionModal("test.RegAction").getForwardModal("failed").getPath());    }

控制台打印结果

/regActiontest.RegAction/reg.jsp

转载于:https://www.cnblogs.com/IT-CPC/p/10942681.html

你可能感兴趣的文章
Graphical installers are not supported by the VM解决办法
查看>>
我的大学之路---2012在迷雾中前进
查看>>
FF下margin不起作用的问题
查看>>
Mysql的几个成功故事
查看>>
Syngress.Nmap.in.the.Enterprise.Your.Guide.to.Network.Scanning
查看>>
小学生都能看懂的表达式计算(图解)
查看>>
物联网有没有创新的思维模式?如果有,会是什么?
查看>>
微会动微信现场互动:年会策划之用产品思维搞定年会
查看>>
利用STP生成树协议实现负载均衡
查看>>
给定一个串,去掉连续的重复字母,
查看>>
我的友情链接
查看>>
MSR2010配置小记
查看>>
微信video标签全屏无法退出bug
查看>>
[转]PostgreSQL 中文资料汇总
查看>>
那些被疯狂追求的女孩,后来怎么样了?
查看>>
(转载)Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)...
查看>>
孟岩:通证(token)和通证经济的目的在于改善现有经济的效率性
查看>>
杜鹃演绎奢华春装大片
查看>>
mongoDb
查看>>
HTML框架1
查看>>