XML建模

目录

  1. 什么叫XML建模
  2. XML建模
  3. 思路
  4. 案例

 

一:什么叫XML建模

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

二:XML建模

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

三:思路

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配置文件

四:案例

导入必要的jar包

dom4j-1.6.1.jar

jaxen-1.1-beta-6.jar

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>

实体类 

 ForwardModel.java

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 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;
	}
	
	public ForwardModel() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
	}

	
	
	
}

ActionModel.java

package com.zking.xml.entity;

import java.io.Serializable;
import java.util.HashMap;
/**
 * xml建模  action标签对应的对象模型类
 * @author Administrator
 *
 */
import java.util.Map;
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 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, ForwardModel> getForwards() {
		return forwards;
	}
	public void setForwards(Map<String, ForwardModel> forwards) {
		this.forwards = forwards;
	}
	public ActionModel() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "ActionModel [path=" + path + ", type=" + type + "]";
	}
	
	/**
	 * 将forwordModel对象放入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.getForwards().get(name);
	}
}

 ConfigModel.java

package com.zking.xml.entity;

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

	/**
	 * 
	 */
	private static final long serialVersionUID = -4636993202614324739L;
	
	//存放config节点下的action对象模型的集合,以action的path属性为键,ActionModel对象为值
	private Map<String, ActionModel> actions = new HashMap<String, ActionModel>();
	
	public Map<String, ActionModel> getActions() {
		return actions;
	}
	public void setActions(Map<String, ActionModel> actions) {
		this.actions = actions;
	}
	public ConfigModel() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "ConfigModel [actions=" + actions + "]";
	}
	
	/**
	 * 将ActionModel对象放入map集合中的方法
	 * @param actionModel 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);
	}
}

读取配置文件

ConfigModelFactory.java

package com.zking.xml.utils;
/**
 * 读取配置文件,将配置信息存储到ConfigModel对象中
 * @author Administrator
 *
 */

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.ActionModel;
import com.zking.xml.entity.ConfigModel;
import com.zking.xml.entity.ForwardModel;

public class ConfigModelFactory {
	
	public static final String DEFAULT_PATH="/config.xml";

	/**
	 * 私有构造方法,不能在外部实例化对象
	 */
	private ConfigModelFactory() {
		
	}
	
	
	
	/**
	 * 读取配置文件信息,将信息存储到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));
					
					//将forwaModel对象存入到ActionMode的集合中
					actionModel.push(forwardModel);
					
				}
				
				//将actionModel对象存入到ConfigModel对象的集合中
				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());
		System.out.println(forwardModel.getRedirect());
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值