07.XML建模

1 篇文章 0 订阅
本文介绍了XML建模的过程,包括根据XML配置文件创建对象模型,如ConfigModel、ActionModel和ForwardModel,并利用dom4j和xpath技术进行建模。详细讲述了如何解析XML配置文件,使用Map集合存储子节点元素,并展示了对应的实体类代码。
摘要由CSDN通过智能技术生成

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

2. XML建模
   1)根据XML配置文件元素节点创建元素节点实体类
   ConfigModel、ActionModel、ForwardModel
   2)利用dom4j+xpath技术实现XML建模
   ConfigModelFactor

思路:
1)xml文件config.xml

2)根据XML中元素节点情况(DTD)来定义ConfigModel、ActionModel、ForwardModel对象模型
   
   A.config节点下有多个子action节点,无节点属性
   B.action节点下有多个子forward节点,有节点属性
   C.forward下无子节点,有节点属性

3)使用Map集合存放子节点元素,其中key为子节点唯一属性,value为整个子节点对象


4)利用工厂模式+dom4j+xpath解析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>

package com.zking.xml.entity;

import java.io.Serializable;

/**
 * xml模型   forward标签对应的对象模型类
 * @author Administrator
 *
 */
public class ForwardModel implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 6235025382426570108L;

    private String name;
    private String path;
    private Boolean redirect;
    public ForwardModel() {
        // TODO Auto-generated constructor stub
    }
    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 getRedirect() {
        return redirect;
    }
    public void setRedirect(Boolean redirect) {
        this.redirect = redirect;
    }
    @Override
    public String toString() {
        return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
    }
}
 

package com.zking.xml.entity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
 * xml模型   action标签对应的对象模型类
 * @author Administrator
 *
 */
public class ActionModel implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -6490183796991304385L;
    
    private String path;
    private String type;
    
    //存放action节点下的forward对象模型的集合,以forward的name属性值为键,forwardModel对象为值
    private Map<String, ForwardModel> forwards =
            new HashMap<String, ForwardModel>();
    
    
    public ActionModel() {
        // TODO Auto-generated constructor stub
    }
    
    
    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 "ActionModel [path=" + path + ", type=" + type + "]";
    }


    public Map<String, ForwardModel> getForwards() {
        return forwards;
    }


    public void setForwards(Map<String, ForwardModel> forwards) {
        this.forwards = forwards;
    }
    
    /**
     * 键forwardModel对象放入map集合中的方法
     * @param forwardModel  ForwardModel对象
     */
    public void push(ForwardModel forwardModel) {
        this.forwards.put(forwardModel.getName(), forwardModel);
    }
    
    /**
     * 通过forward的name从map集合中取出对应的ForwardModel对象
     * @param name
     * @return
     */
    public ForwardModel getforwardModel(String name) {
        return this.forwards.get(name);
    }
    
}
 

package com.zking.xml.entity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * xml模型   config标签对应的对象模型类
 * @author Administrator
 *
 */
public class ConfigModel implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -4636993202614324739L;

    /**
     * action对应的对象模型的集合    以action中的path属性为键,ActionModel对象为值
     */
    private Map<String, ActionModel> actions=
            new HashMap<String, ActionModel>();
    
    public ConfigModel() {
        // TODO Auto-generated constructor stub
    }

    public Map<String, ActionModel> getActions() {
        return actions;
    }

    public void setActions(Map<String, ActionModel> actions) {
        this.actions = actions;
    }

    @Override
    public String toString() {
        return "ConfigModel [actions=" + actions + "]";
    }
    
    /**
     * 将ActionModel对象放入到map集合中
     * @param actionModel
     */
    public void push(ActionModel actionModel) {
        this.actions.put(actionModel.getPath(), actionModel);
    }

    /**
     * 通过path属性值从map集合中找到对应的ActionModel对象
     * @param path
     * @return
     */
    public ActionModel getactionModel(String path) {
        return this.actions.get(path);
    }
    
}
 

package com.zking.xml.biz.util;

import java.io.InputStream;
import java.util.List;

import javax.management.RuntimeErrorException;

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

import com.zking.xml.entity.ActionModel;
import com.zking.xml.entity.ConfigModel;
import com.zking.xml.entity.ForwardModel;

/**
 * 读取配置文件,将配置信息存储到ConfigModel对象中
 * @author Administrator
 *
 */
public class ConfigModelFactory {
    
    public static final String DEFAULT_PATH ="/config.xml";
    
    
    /**
     * 私有构造方法,不能在外部实例化对象
     */
    private ConfigModelFactory() {
        
    }
    
    /**
     * 实例方法    对象.test()调用
     */
    public void test() {
        
    }
    
    /**
     * 读取配置文件信息,将信息存储到configModel对象中
     * @return
     */
    public static ConfigModel createConfigModel(String path) {
        //一.解析
        //1.获取io流
        InputStream is = ConfigModelFactory.class.getResourceAsStream(path);
        
        //定义需要的变量
        ActionModel actionModel =null;
        String actionPath = null;
        String actionType = null;
        
        String forwardname =null;
        String forwardPath =null;
        String forwardredirect =null;
        
        ForwardModel forwardModel =null;
        ConfigModel configModel = new ConfigModel();
        
        //2.创建xml读取工具类SaxReader
        SAXReader sr =new SAXReader();
        
        try {
            //3.读取配置文件,读取document对象
            Document doc = sr.read(is);
            
            //4.找到action节点
            List<Node> actionNodes = doc.selectNodes("/config/action");
            
            //5.遍历action节点,获取节点中的path和type属性值
            for (Node actionNode : actionNodes) {
                //将节点对象强转成元素对象
                Element nodeEl =(Element)actionNode;
                
                //获取属性值
                actionPath = nodeEl.attributeValue("path");
                actionType = nodeEl.attributeValue("type");
                
                //实例化ActionModel对象
                actionModel = new ActionModel();
                
                //将属性值放入到actionModel对象中
                actionModel.setPath(actionPath);
                actionModel.setType(actionType);
                
                //找到该action节点下的forward节点
                List<Node> forwardNodes = actionNode.selectNodes("forward");
                
                //遍历forward节点
                for (Node forwardNode : forwardNodes) {
                    //将节点对象转换成元素对象
                    Element forwardEl = (Element)forwardNode;
                    
                    //获取forward的属性值
                    forwardname = forwardEl.attributeValue("name");
                    forwardPath = forwardEl.attributeValue("path");
                    forwardredirect = forwardEl.attributeValue("redirect");
                    
                    //实例化ForwardModel对象
                    forwardModel = new ForwardModel();
                    
                    //将xml文件中解析出来的值放入到对象属性中
                    forwardModel.setName(forwardname);
                    forwardModel.setPath(forwardPath);
                    forwardModel.setRedirect(Boolean.parseBoolean(forwardredirect));
                    
                    //将forwardModel对象存入到ActionMode的集合中
                    actionModel.push(forwardModel);
                }
                
                //将actionModel对象存入到actionModel对象的集合中
                configModel.push(actionModel);
                
            }
            return configModel;
            
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
        
    }
    
    
    public static ConfigModel createConfigModel() {
    
        return createConfigModel(DEFAULT_PATH);
    }
    
    public static void main(String[] args) {
        ConfigModel configModel = ConfigModelFactory.createConfigModel();
        
        ActionModel actionModel = configModel.getactionModel("/regAction");
        
        System.out.println(actionModel.getType());
        
        ForwardModel forwardModel = actionModel.getforwardModel("success");
        
        System.out.println(forwardModel.getPath());
    }
    
    
    
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值