XML建模

目录

1.什么叫XML建模

2. XML建模

思路:1)xml文件config.xml

2)根据XML中元素节点情况(DTD)来定义ConfigModel、ActionModel、ForwardModel对象模型

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

4)利用工厂模式+dom4j+xpath解析Xml配置文件


1.什么叫XML建模

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

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

代码展示

1.xml的代码

	<!DOCTYPE config[
		<!ELEMENT config (action+)>
		<!ELEMENT action (forward+)>
		<!ELEMENT forward (#PCDATA)>
		
		<!ATTLIST action 
			path CDATA #IMPLIED 
			type CDATA #IMPLIED
		>
		<!ATTLIST forward
			name CDATA #IMPLIED
			path CDATA #IMPLIED
			redirect (true|false) "true"
		 >
	]>
	<!--
		config标签:可以包含0~N个action标签
	-->
<config>
	<!--
		action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 ,子控制器对应的路径
		type:字符串,非空,子控制器的完整类名
	-->
	<action path="/registerAction" type="test.action.RegisterAction">
		<forward name="success" path="/index.jsp" redirect="true" />
		<forward name="failed" path="/register.jsp" redirect="false" />
	</action>
	<action path="/loginAction" type="test.action.LoginAction">
		<forward name="a" path="/index.jsp" redirect="false" />
		<forward name="b" path="/welcome.jsp" redirect="true" />
	</action>
</config>

2.三个实体类的代码

2.1action类

package com.zking.entiy;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
 * 	action模型类
 * @author zjjt
 *
 */
public class Action implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1528794090846487059L;
	private String path;
	private String type;
	private Map<String, Forward> forward=new HashMap<String,Forward>();
	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, Forward> getForward() {
		return forward;
	}
	public void setForward(Map<String, Forward> forward) {
		this.forward = forward;
	}
	public Action() {
		// TODO Auto-generated constructor stub
	}
	//用来增加的方法
	public void push(Forward forward) {
		this.forward.put(forward.getName(), forward);
	}
	//用来查询的方法
	public Forward getforward(String name) {
		return this.forward.get(name);
	}
}

2.2config类

package com.zking.entiy;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
 * 	Config模型类
 * @author zjjt
 *
 */
public class Config implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7820328191550440591L;
	private Map<String, Action> action=new HashMap<String, Action>();
	public Map<String, Action> getAction() {
		return action;
	}
	public void setAction(Map<String, Action> action) {
		this.action = action;
	}
	public Config() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Config [action=" + action + "]";
	}
	/**
	 * 	用来增加的方法
	 * @param action
	 */
	public void push(Action action) {
		this.action.put(action.getPath(), action);
	}
	//用来查询的方法
	public Action getaction(String path) {
		return this.action.get(path);
	}
}

2.3forward类

package com.zking.entiy;

import java.io.Serializable;
/**
 * 	Forward模型类
 * @author zjjt
 *
 */
public class Forward  implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1763053502472705461L;
	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 Forward() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Forward [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
	}
}

3.工具类configmodl

package com.zking.uilt;

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.entiy.Action;
import com.zking.entiy.Config;
import com.zking.entiy.Forward;

public class ConfigModl {
	//设置path静态常量
	public static final String path="/config.xml";
	//吧构造方法私有化
	private ConfigModl() {
		
	}
	//用来接收传递数据
	public static String actionpath;
	public static String actiontype;
	public static Action action;
	public static String forwardname;
	public static String forwardpath;
	public static String forwardredirect;
	public static Forward forward;
	public static Config config=new Config();
	
	public static Config getconfig(String path) throws Exception {
		//获取io流
		InputStream in1 = ConfigModl.class.getResourceAsStream(path);
		//创建xml读取工具类
		SAXReader sr=new SAXReader();
		//读取配置文件,获取Document对象
		Document doc = sr.read(in1);
		//找到action节点
		List<Node> actionlist = doc.selectNodes("/config/action");
		//遍历action集合获取path和type属性值
		for (Node a : actionlist) {
			//将节点对象强转换成元素对象
			Element el=(Element)a;
			//获取属性值
			actionpath = el.attributeValue("path");
			actiontype = el.attributeValue("type");
			//实例化action
			action=new Action();
			//将数据放入action对象中
			action.setPath(actionpath);
			action.setType(actiontype);
			//找到action下的forward节点
			List<Node> selea = a.selectNodes("forward");
			//遍历forward节点
			for (Node na : selea) {
				//将节点对象强转换成元素对象
				Element lea=(Element)na;
				//获取属性值
				forwardname = lea.attributeValue("name");
				forwardpath = lea.attributeValue("path");
				forwardredirect = lea.attributeValue("redirect");
				//实例化forward
				forward=new Forward();
				//将数据放入forward对象中
				forward.setName(forwardname);
				forward.setPath(forwardpath);
				forward.setRedirect(Boolean.parseBoolean(forwardredirect));
				//将获取的属性放入action中
				action.push(forward);
			}
			//将获取的数据放入config中
			config.push(action);
		}
		return config;
	}
	public static Config getconfig() throws Exception {
		return getconfig(path);
	}
	public static void main(String[] args) throws Exception {
		Config a =ConfigModl.getconfig();
		System.out.println(a);
	}
}

带吗最后展示

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃亏了的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值