你不知道的XML建模

目录

思维导图:​

一,什么是XML建模?

二,关于XML建模

1.建模步骤

建模可分以下两步骤:

根据XML配置文件元素节点创建元素节点实体类

利用dom4j+xpath技术实现XML建模

同时我们还可以建立自己的异常体系

总结:


思维导图:

一,什么是XML建模?

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

二,关于XML建模

1.建模步骤

建模可分以下两步骤:

  • 根据XML配置文件元素节点创建元素节点实体类
       ConfigModel、ActionModel、ForwardModel

  • 利用dom4j+xpath技术实现XML建模
       ConfigModelFactory

config,action,可以新增,删除,获取

建模,由内到外

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

  • config节点下有多个子action节点,无节点属性
  • action节点下有多个子forward节点,有节点属性
  • forward下无子节点,有节点属性 

接下来让我们通过代码进一步理解 

<?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:字符串,非空 -->
	<!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串 redirect:只能是false|true,允许空,默认值为false -->

	<action path="/studentAction02" type="org.lisen.mvc.action.StudentAction">
		<forward name="students" path="/students/studentList.jsp" redirect="false"/>
	</action>
</config>

config.xml中action标签里的path和forward标签里的name相当于ID

根据XML配置文件元素节点创建元素节点实体类

ConfigModel.java 

public class ConfigModel {
	
//	一个config节点下可以有多个action节点
//	放key==path和value==ActionModel类/对象:通过根节点找到action
//	初始化:new HashMap<>()
	private Map<String, ActionModel> actionMap=new HashMap<>();
	
	/**
	 * 将path放入key,保持path不重复
	 * 遍历ActionModel对象,通过ActionModel对象获取path属性
	 * @param 传入ActionModel这个类/对象
	 */
    public void put(ActionModel action) {
    	if(actionMap.containsKey(action.getPath())) {
    		throw new ActionDuplicateDefinitionException("Action path= "+action.getPath()+" Duplicate definition");
    	}
    	actionMap.put(action.getPath(), action);
    }
    
    /**
     * 找path
     * @param 传入path
     * @return 找不到path则抛出异常,找到就返回path
     */
    public ActionModel find(String path) {
    	if(!actionMap.containsKey(path)) {
    		throw new ActionNotFoundException("Action path ="+path+" not found");
    	}
    	return actionMap.get(path);
    }
    
}

 ActionModel.java

public class ActionModel {
	
//	有两个节点
	private String type;
	private String path;
	
//	包含元素forward:通过action找到forward
	private Map<String, ForwardModel> forwardMap =new HashMap<>();
	
//	将name放入key,保持name不重复
	public void put(ForwardModel forward) {
		if(forwardMap.containsKey(forward.getName())) {
			throw new ForwardDuplicateDefinitionException("forward name = "+forward.getName()+" DuplicateDefinition");
		}
		forwardMap.put(forward.getName(), forward);
	}
	
//	找name
	public ForwardModel find(String name) {
		if(!forwardMap.containsKey(name)) {
			throw new ForwardNotFoundException("forward name ="+name+" not found");
		}
		return forwardMap.get(name);
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
 
	@Override
	public String toString() {
		return "ActionModel [type=" + type + ", path=" + path + ", forwardMap=" + forwardMap + "]";
	}	
}

 ForwardModel.java

public class ForwardModel extends RuntimeException{
	//三个属性
	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;
	}
	
	//将redirect中的字符串转成boolean类型的方法
	public void setRedirect(String redirect) {
		//this=当前类
		this.redirect =Boolean.valueOf(redirect);
	}
	
	@Override
	public String toString() {
		return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
	}	
}

利用dom4j+xpath技术实现XML建模

ConfigModelFactory

/**
 * 单例工厂类
 * 让解析XML的代码只执行一次
 */
@SuppressWarnings("unchecked")
public final class ConfigModelFactory {
	//单例模式
	private ConfigModelFactory() {}
	//根节点模型
	private static ConfigModel config = new ConfigModel();
	//只读取一次config中的数据,并填充到模型中
	static {
		try {
		//读
		InputStream is=XmlReader.class.getResourceAsStream("/config.xml");
		SAXReader reader=new SAXReader();
		
		Document doc = reader.read(is);
		//通过根元素获得根元素下的节点action(xml中的数结构!)
		Element rootElement = doc.getRootElement();
		List<Element> actions = rootElement.selectNodes("action");
		
		for (Element e : actions) {
			String path = e.attributeValue("path");
			String type = e.attributeValue("type");
			
			//放入模型
			ActionModel action=new ActionModel();
			action.setPath(path);
			action.setType(type);
			
			//通过action元素获得actions下面的节点forward
			List<Element> forward1 = e.selectNodes("forward");
			for (Element f : forward1) {
				String name = f.attributeValue("name");
				String fPath = f.attributeValue("path");
				String redirect = f.attributeValue("redirect");
				
				//放入模型
				ForwardModel forward=new ForwardModel();
				forward.setName(name);
				forward.setPath(fPath);
				forward.setRedirect(redirect);
				//将forward放入action
				action.put(forward);
				}
			//循环完成后将action放入根节点
			config.put(action);
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	public static ConfigModel getConfig() {
		return config;
	}
	
    //测试
	public static void main(String[] args) {
		ConfigModel config = ConfigModelFactory.getConfig();
		//通过action中的path(xml中有/)找action
		ActionModel action = config.find("/studentAction");
		System.out.println(action);
		//通过forward中的name找forward,xml中无/
		ForwardModel forward = action.find("students");
		System.out.println(forward);
	}
	
}

同时我们还可以建立自己的异常体系

ActionDuplicateDefinitionException:Action元素中存在重复定义异常

public class ActionDuplicateDefinitionException extends RuntimeException {	
	public ActionDuplicateDefinitionException() {
		super();
	}	
	public ActionDuplicateDefinitionException(String msg) {
		super(msg);
	}	
	public ActionDuplicateDefinitionException(String msg, Throwable c) {
		super(msg, c);
	}
}

ActionNotFoundException:未找到Action异常

public class ActionNotFoundException extends RuntimeException {	
	public ActionNotFoundException() {
		super();
	}	
	public ActionNotFoundException(String msg) {
		super(msg);
	}	
	public ActionNotFoundException(String msg, Throwable c) {
		super(msg, c);
	}
}

ForwardDuplicateDefinitionException:Forward元素中存在重复定义异常 

public class ForwardDuplicateDefinitionException extends RuntimeException {	
	public ForwardDuplicateDefinitionException() {
		super();
	}	
	public ForwardDuplicateDefinitionException(String msg) {
		super(msg);
	}	
	public ForwardDuplicateDefinitionException(String msg, Throwable c) {
		super(msg, c);
	}
}

 ForwardNotFoundException:未找到Forward异常

public class ForwardNotFoundException extends RuntimeException {	
	public ForwardNotFoundException() {
		super();
	}
	public ForwardNotFoundException(String msg) {
		super(msg);
	}	
	public ForwardNotFoundException(String msg, Throwable c) {
		super(msg, c);
	}
}

总结:

今天的知识分享就到此为止啦,精彩下期继续! 😁😁

  • 19
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值