XML-xml建模

XML-xml建模

建模的由来
就是将指定的xml字符串当作对象来操作
如果说当对一个指定的xml格式字符串完成了建模操作,
好处在于,只需要调用指定的方法就可以完成预定的字符串获取;

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

好处:
提高代码的复用性


下面展示一些 内联代码片
在这里插入图片描述

创建三个基础类

package com.lrc.jianmo;

import java.util.HashMap;
import java.util.Map;


public class ActionModel {
	private String path;
	private String type;
	private Map<String, ForwardModel> fm=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 fl) {
		fm.put(fl.getName(), fl);
	}
	/**
	 * 弹栈
	 *
	 */
	
	public ForwardModel pop(String name) {
		return null;
	}
	
}


package com.lrc.jianmo;

import java.util.HashMap;
import java.util.Map;

public class ConfigModel {
	private static Map<String, ActionModel> am=new HashMap<>();
	
	public void push(ActionModel al) {
		am.put(al.getPath(), al);
	}
	public static ActionModel pop(String path) {
		return am.get(path);
	}
	
}


package com.lrc.jianmo;

public class ForwardModel {
	
	private String name;
	private String path;
	private boolean redirect=true;
	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;
	}
	
}


创建23中模式中的工厂模式

package com.lrc.jianmo;

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

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

/**
 * java中有23中设计模式
 * 这是其中的一种
 * 工厂模式
 * 		why:
 * 			能够提高代码的复用性
 * 		how:
 * 			只要建立一个方法,生产指定的你需要的对象
 * 		where:
 * 			生产指定的需要的对象,以便重复使用
 * 
 */


public class ConfigModelFactory {
	/**
	 * 默认资源文件mvc.xml是放在建模类的同包下
	 * @throws DocumentException 
	 *
	 */
	public static ConfigModel build() throws DocumentException {
		return build("mvc.xml");
	}
	/**
	 * 当资源文件需要手动改变位置的情况下,需要调用下面方法
	 * @throws DocumentException 
	 *
	 */
	public static ConfigModel build(String xmlPath) throws DocumentException {
		ConfigModel cm=new ConfigModel();
		ActionModel am=null;
		ForwardModel fm=null;
		InputStream in= ConfigModelFactory.class.getResourceAsStream(xmlPath);
		SAXReader re=new SAXReader();
		Document doc=re.read(in);
		List<Element> acEles=doc.selectNodes("config/action");
		for (Element acEle : acEles) {
			am=new ActionModel();
			//填充am
			am.setPath(acEle.attributeValue("path"));
			am.setType(acEle.attributeValue("type"));
			List<Element> fowaEles= acEle.selectNodes("forward");
			for (Element fowaEle : fowaEles) {
				fm=new ForwardModel();
				//填充fm
				fm.setName(fowaEle.attributeValue("name"));
				fm.setPath(fowaEle.attributeValue("path"));
				fm.setRedirect(!"false".equals(fowaEle.attributeValue("redirect")));
				am.push(fm);
			}
			cm.push(am);
		}
		return cm;
	}
	
	public static void main(String[] args) throws DocumentException {
		ConfigModel cm=ConfigModelFactory.build();
		ActionModel am=ConfigModel.pop("/loginAction");
		System.out.println(cm.pop("/loginAction").getType());
		//System.out.println(am.pop("success").getPath());
	}
	
}

代码效果显示如下
在这里插入图片描述

建模分两步
1、以面向对象的编程思想,描述xml资源文件
2、将xml文件中内容封装进model实体对象

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值