xml建模

本文介绍了XML建模的概念,即把XML配置文件中的元素、属性、文本转换为对象的过程。详细阐述了XML建模的思路,并通过config.xml、ForwardModul、ActionModul、ConfigModul等案例进行说明,同时提供了工具类和运行效果的展示。
摘要由CSDN通过智能技术生成

一、什么叫XML建模

    将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模

二、XML建模思路图

 
三、案例

config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE config[
        <!ELEMENT config (action*)>
        <!ELEMENT action (forward*)>
        <!ELEMENT forward EMPTY>
        <!ATTLIST action
            path CDATA #REQUIRED
            type CDATA #REQUIRED
        >
        <!ATTLIST forward
            name CDATA #REQUIRED
            path CDATA #REQUIRED
            redirect (true|false) "false"
        >
    ]>
        <!--
            config标签:可以包含0~N个action标签
        -->
    <config>
        <!--
            action标签:可以饱含0~N个forward标签
            path:以/开头的字符串,并且值必须唯一 非空
            type:字符串,非空
        -->
        <action path="/regAction" type="test.RegAction">
            <!--
                forward标签:没有子标签;
                name:字符串,同一action标签下的forward标签name值不能相同 ;
                path:以/开头的字符串
                redirect:只能是false|true,允许空,默认值为false
            -->
            <forward name="failed" path="/reg.jsp" redirect="false" />
            <forward name="success" path="/login.jsp" redirect="true" />
        </action>
     
        <action path="/loginAction" type="test.LoginAction">
            <forward name="failed" path="/login.jsp" redirect="false" />
            <forward name="success" path="/main.jsp" redirect="true" />
        </action>
    </config>

ForwardModul

   

 package com.zking.xml.entity;
     
    import java.io.Serializable;
     
    public class ForwardModul implements Serializable {
     
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        private String name;
        private String path;
        private Boolean redirect;
        public ForwardModul() {
            
        }
        public String getNmae() {
            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 getRedirect() {
            return redirect;
        }
        public void setRedirect(Boolean redirect) {
            this.redirect = redirect;
        }
        @Override
        public String toString() {
            return "ForwardModul [name=" + name + ", type=" + path + ", redirect=" + redirect + "]";
        }
        
    }

ActionModul

   package com.zking.xml.entity;
     
    import java.io.Serializable;
    import java.util.HashMap;
    import java.util.Map;
     
    public class ActionModul implements Serializable {
     
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        private String path;
        private String type;
        private Map<String,ForwardModul> ActionMpdul = new
        HashMap<String, ForwardModul>();
        /**
         * 增加ForwardModul对象
         * @param forwardModul
         */
        public void push(ForwardModul forwardModul) {
            this.ActionMpdul.put(forwardModul.getNmae(),forwardModul);
        }
        
        /**
         * 查找对应的对象
         * @param name 根据键拿到值
         * @return
         */
        public ForwardModul getForword(String name) {
            return this.ActionMpdul.get(name);
        }
        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;
        }
        public Map<String, ForwardModul> getActionMpdul() {
            return ActionMpdul;
        }
        public void setActionMpdul(Map<String, ForwardModul> actionMpdul) {
            ActionMpdul = actionMpdul;
        }
        public ActionModul() {
            // TODO Auto-generated constructor stub
        }
    }

ConfigModul

  

  package com.zking.xml.entity;
     
    import java.io.Serializable;
    import java.util.HashMap;
    /**
     * 建模实体类 config
     * @author zjjt
     *
     */
    import java.util.Map;
    public class ConfigModul implements Serializable{
     
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        
        private Map<String,ActionModul> ConfigModul =
                new HashMap<String, ActionModul>();
     
        public Map<String, ActionModul> getConfigModul() {
            return ConfigModul;
        }
     
        public void setConfigModul(Map<String, ActionModul> configModul) {
            ConfigModul = configModul;
        }
        public ConfigModul() {
            // TODO Auto-generated constructor stub
        }
        
        /**
         * 将actionModul对象存到Config集合中
         * @param Actionmodul
         */
        public void push(ActionModul Actionmodul) {
            this.ConfigModul.put(Actionmodul.getPath(),Actionmodul);
        }
        
        /**
         * 查找 根据键拿到值
         * @param path
         * @return
         */
        public ActionModul getActionMOdul(String path) {
            return this.ConfigModul.get(path);
        }
    }

工具类

   

package com.zking.xml.util;
     
    import java.io.InputStream;
    import java.util.List;
     
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;
     
    import com.zking.xml.entity.ActionModul;
    import com.zking.xml.entity.ConfigModul;
    import com.zking.xml.entity.ForwardModul;
     
    public class JianMo {
        private static final String PATH = "/config.xml";
        private JianMo() {
            
        }
        public static ConfigModul getModel(String path) {
            //获取io流
            InputStream is = JianMo.class.getResourceAsStream(path);
            //实例化解析xml工具类 SAXReader
            SAXReader srd = new SAXReader();
            //实例化
            String ActionPath = null;
            String Actiontype = null;
            ActionModul Actionmodul = null;
            
            String ForwardName = null;
            String ForwardPath = null;
            String Forwardredirect = null;
            ForwardModul forwardmodel = null;
            ConfigModul configModul = new ConfigModul();
            try {
                //读取xml文件 得到document对象
                Document document = srd.read(is);
                //找到config节点下的anction
                List<Node> ActionNodes = document.selectNodes("/config/action");
                //System.out.println(ActionNodes.size());
                for (Node node : ActionNodes) {
                    //将文本对象转为元素对象
                    Element el = (Element)node;
                    //拿属性值根据属性名拿对的属性值
                    ActionPath = el.attributeValue("path");
                    Actiontype = el.attributeValue("type");
                    Actionmodul = new ActionModul();
                    Actionmodul.setPath(ActionPath);
                    Actionmodul.setType(Actiontype);
                    //获取action节点下的forword节点
                    List<Node> forwardNodes = node.selectNodes("forward");
                    for (Node node2 : forwardNodes) {
                        //将文本对象转为元素对象
                        Element forwardnel = (Element)node2;
                        //拿属性值根据属性名拿对的属性值
                        ForwardName = forwardnel.attributeValue("name");
                        ForwardPath = forwardnel.attributeValue("path");
                        forwardmodel = new ForwardModul();
                        Forwardredirect = forwardnel.attributeValue("redirect");
                        forwardmodel.setName(ForwardName);
                        forwardmodel.setPath(ForwardPath);
                        forwardmodel.setRedirect(Boolean.parseBoolean(Forwardredirect));
                        Actionmodul.push(forwardmodel);
                    }
                    configModul.push(Actionmodul);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return configModul;
        }
        
        public static ConfigModul getModel() {
            return getModel(PATH);
        }
        public static void main(String[] args) {
            ConfigModul model2 = getModel();
            //根据键拿到ActionModul对象 这个值也就是xml里面所对应的path值
            ActionModul actionMOdul = model2.getActionMOdul("/regAction");
      //根据键拿到ForwordModul对象 这个值也就是xml里面所对应的name值
            ForwardModul forword = actionMOdul.getForword("failed");
            System.out.println(actionMOdul.getType());
            System.out.println(forword.getNmae()+""+forword.getPath());
        }
    }    

运行效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值