XML建模

XML建模

一:XML建模

**建模的核心就是利用java面向对象的特性,用操作对象的方式来操作XML**

二:XML建模思路步骤

  		1、分析需要被建模的文件中有几个对象(标签);
		2、分析对象拥有的行为以及属性;
		3、注意:定义对象从小到大 ,从里到外;
		4、通过设计模式中的工厂模式,解析xml生产出指定对象。	

三:XML实例演示

3.1 解析资源文件

<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>

3.2 需求

3.3 分析
在这里插入图片描述
3.4 ForwardModel类

public class ForwardModel {
//	<forward name="failed" path="/reg.jsp" redirect="false" />
	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 isRedirect() {
		return redirect;
	}
	public void setRedirect(boolean redirect) {
		this.redirect = redirect;
	}
}

3.5 ActionModel类
将指定的forwardModel压入当前actionModel对象中

public class ActionModel {
//<action path="/regAction" type="test.RegAction">
	private String path;
	private String type;
	private Map<String, ForwardModel> forMap=new HashMap<>();
	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 void push(ForwardModel forwardModel) {
		 forMap.put(forwardModel.getName(), forwardModel);
	}
	public ForwardModel pop(String name) {
		 return forMap.get(name);
	}
}

3.6 ConfigModel类

  **将指定的actionModel压入当前configModel对象中**
public class ConfigModel {
	private Map<String, ActionModel> acMap=new HashMap<String, ActionModel>();
	public void push(ActionModel actionModel) {
		 acMap.put(actionModel.getPath(), actionModel);
	}
	public ActionModel pop(String path) {
		 return acMap.get(path);
	}
}

3.7 ConfigModelFactory类

	  **工厂模式解决问题   将代码封装  提高代码的复用性
	 一般工厂类一定会有一个方法  就是生产指定模型对象的方法**
public class ConfigModelFactory {
//	动态读取任意位置下的构架配置文件
	public static ConfigModel build(String path) throws Exception {
		InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
		SAXReader reader=new SAXReader();
//		doc--->/config.xml的内容
		Document doc = reader.read(in);
//		把内容填充到configModel对象中
		ConfigModel configModel=new ConfigModel();
		ActionModel actionModel=null;
		ForwardModel forwardModel=null;
		List<Element> actionLs = doc.selectNodes("/config/action");
		for (Element actionL : actionLs) {
			actionModel=new ActionModel();
			actionModel.setPath(actionL.attributeValue("path"));
			actionModel.setType(actionL.attributeValue("type"));
//			拿到forward标签内容
			List<Element> forwardLs = actionL.selectNodes("forward");
			for (Element forwardL : forwardLs) {
				forwardModel =new ForwardModel();
				forwardModel.setName(forwardL.attributeValue("name"));
				forwardModel.setPath(forwardL.attributeValue("path"));
				forwardModel.setRedirect(!"false".equals(forwardL.attribute("redirect")));
			}
			configModel.push(actionModel);
		}
		return configModel;
	}
//	默认框架路径的模型对象构建方法
	public static ConfigModel build() throws Exception {
		return build("config.xml");
	}
}

3.8 运行结果
在这里插入图片描述

四:总结

请大家多多指教!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值