XML系列——建模

hello,家人们,我又来啦,哈哈,我们今天继续讲XML系列之建模

目录

一.XML建模

1.XML建模的由来

2.XML建模的思路

3.XML建模的核心思想

4.XML建模的应用

二.工厂模式


一.XML建模

  • 1.XML建模的由来

就是将指定的xml文件字符串当作对象来操作,如果说当 对一个指定的xml格式字符串完成了建模操作,那么只需要调用指定的方法就可以完成预定的字符串获取.

  • 2.XML建模的思路

<1>、分析需要被建模的文件中有那几个对象

<2>、每个对象拥有的行为以及属性

<3>、定义对象从小到大(从里到外)

<4>、通过23种的设计模式中的工厂模式,解析xml生产出指定对象

<5>、好处:提高代码的复用性

  • 3.XML建模的核心思想

xml建模的核心思想就是利用java面对对象的特性,用操作对象的方式来操作xml

  • 4.XML建模的应用

在XML建模中用几个类来获取 config.xml中的属性

注:属性为String类型,子元素标签则是map的值,子元素标签的唯一标识则为map的值

       建模是由里到外

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/studentAction"
		type="org.lisen.mvc.action.StudentAction">
		<forward name="students" path="/students/studentList.jsp"
			redirect="false" />
	</action>
	
	<action path="/studentAction02"
		type="org.lisen.mvc.action.StudentAction">
		<forward name="students02" path="/students/studentList.jsp"
			redirect="false" />
	</action>
</config>

我们根据上面的xml文件,可以得出:

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

我们可以根据XML配置文件元素节点创建元素节点实体类:

   🍍🍍ConfigModel.java

package com.zking.framework;

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

/**
 * MVC建模
 * @author zjjt
 *
 */
public class ConfigModel {
	
	//如果现在使用List,并不安全,如果使用List,找属性必定要用上循环,所以使用Map,
	private Map<String,ActionModel> actionMap=new HashMap<String, ActionModel>();

	
	public void put(ActionModel action) {//需要一些限制
	  if(actionMap.containsKey(action.getPath())) {//这个容器里面有没有包含key为action.getpath的路径,有的话就抛出异常
		  throw new ActionChongFuException("Action path :"+action.getPath()+"chongfu");//抛出异常
	  }
	  actionMap.put(action.getPath(), action);
	}
	

	
	
	public ActionModel  find(String path) {
		if(!actionMap.containsKey(path)) {
			throw new ActionNotFoundException("Action path :"+path+"Not Found");
		}
		return actionMap.get(path);
	}

}

    🍖🍖ActionModel.java

package com.zking.framework;

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

public class ActionModel {

	private String path;
	private String type;
    
	private Map<String, ForwardModel> forwardMap=new HashMap<String, ForwardModel>();
	
	public ActionModel() {
		
	}


	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 put(ForwardModel f) {
		if(forwardMap.containsKey(f.getName())) {
			throw new ForwardChongFuException("forward name:"+f.getName()+"chong fu");
		}
		forwardMap.put(f.getName(), f);
	}
	
	
	public ForwardModel  find(String name) {
		if(!forwardMap.containsKey(name)) {
			throw new ForwardNotFoundException("forward name :"+name+"Not Found");
		}
		return forwardMap.get(name);
	}


	@Override
	public String toString() {
		return "ActionModel [path=" + path + ", type=" + type + ", forwardMap=" + forwardMap + "]";
	}
	
	
	
	
}

    🍉🍉 ForwardModel.java

package com.zking.framework;

public class ForwardModel {
	
	private String name;
	private String path;
	private boolean redirect;
	
	
	
	public ForwardModel() {
		// TODO Auto-generated constructor stub
	}
	
	
	
	public ForwardModel(String name, String path, boolean redirect) {
		super();
		this.name = name;
		this.path = path;
		this.redirect = 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(String redirect) {
		this.redirect = Boolean.valueOf(redirect);
	}
	
	
	public void setRedirect(boolean redirect) {
		this.redirect = redirect;
	}



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

}


🍟🍟我们还可以自定义异常:

ActionChongFuExceptionAction元素中存在重复异常

package com.zking.framework;
/**
 * 异常:Action重复
 * @author zjjt
 *
 */
public class ActionChongFuException extends RuntimeException{
	
	/**
	 * 直接调用父类的方法
	 */
	public ActionChongFuException() {
		super();
	}
	
	
	public ActionChongFuException(String msg) {
		super(msg);
	}
	
	
	//Throwable是指异常的一个原因
	public ActionChongFuException(String msg,Throwable c) {
	    super(msg, c);	
	}
	

	
	
	
	

}

ActionNotFoundException:Action元素没有找到

package com.zking.framework;
/**
 * 没有找到
 * @author zjjt
 *
 */
public class ActionNotFoundException extends RuntimeException{

	

	/**
	 * 直接调用父类的方法
	 */
	public ActionNotFoundException() {
		super();
	}
	
	
	public ActionNotFoundException(String msg) {
		super(msg);
	}
	
	
	//Throwable是指异常的一个原因
	public ActionNotFoundException(String msg,Throwable c) {
	    super(msg, c);	
	}
}

ForwardChongFuException:Forward元素中存在重复异常

package com.zking.framework;
/**
 * 异常:Forward重复
 * @author zjjt
 *
 */
public class ForwardChongFuException extends RuntimeException{
	
	/**
	 * 直接调用父类的方法
	 */
	public ForwardChongFuException() {
		super();
	}
	
	
	public ForwardChongFuException(String msg) {
		super(msg);
	}
	
	
	//Throwable是指异常的一个原因
	public ForwardChongFuException(String msg,Throwable c) {
	    super(msg, c);	
	}
	
	

}

ForwardNotFoundException:Forward未找到

package com.zking.framework;
/**
 * 没有找到
 * @author zjjt
 *
 */
public class ForwardNotFoundException extends RuntimeException{

	

	/**
	 * 直接调用父类的方法
	 */
	public ForwardNotFoundException() {
		super();
	}
	
	
	public ForwardNotFoundException(String msg) {
		super(msg);
	}
	
	
	//Throwable是指异常的一个原因
	public ForwardNotFoundException(String msg,Throwable c) {
	    super(msg, c);	
	}
}


二.工厂模式

工厂模式解决的问题:将代码封装,提高代码的复用性

🍐🍐通过工厂模式,解析XML生产的指定对象

package com.zking.framework;

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

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

/**
 * 单例模式(饿汉模式)
 * @author zjjt
 *
 */

@SuppressWarnings("unchecked")
public  final  class ConfigDanLi {

	//设置一个私有的构造函数
	private ConfigDanLi() {};


    //实例化一个ConfigModel
	private static ConfigModel c=new ConfigModel();


	
	//在类加载的时候,读取一次就行了(读取config.xml中的数据,填充到模型中)
	static {
		try {
			//主文件(通过自己的类点class,然后括号里要加/,不然就是说要和原文件放入同一个目录中)
			InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
		    
			//xml中有一个帮助类,老各斯缺,哈哈,不懂的就去百度查 dom4j
			SAXReader reader=new SAXReader();
			Document doc = reader.read(in);//报错需要抛出异常
			
			Element rootElement = doc.getRootElement();//首先获取根元素;
			
			//dom4j常用方法:selectNodes
			
			List<Element> actions = rootElement.selectNodes("action");//获得config.xml中的action元素
			
			//遍历所有ations,在action元素中寻找path属性和type属性
			for (Element e : actions) {
				String path=e.attributeValue("path");
				String type=e.attributeValue("type");
				
				ActionModel action=new ActionModel();
				
				
				action.setPath(path);
				action.setType(type);
				
				//forward这个元素不止一个,所以需要一个集合装起来(如果是"/forward"就是表示从根目录开始)
				List<Element> forwards = e.selectNodes("forward");
				
				//然后在集合中寻找name属性和path属性,还有一个redirect属性
				for (Element f : forwards) {
					String name=f.attributeValue("name");
					String fpath = f.attributeValue("path");
				    String redirect = f.attributeValue("redirect");//今天只是读取,所以不需要使用布尔
				     
				    ForwardModel f1=new ForwardModel();
				    f1.setName(name);
				    f1.setPath(fpath);
				    f1.setRedirect(redirect);
				    
				    //建立关系
				    action.put(f1);
				}
				c.put(action);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	public static ConfigModel getConfig() {
		return c;
	}
	
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		ConfigModel con=ConfigDanLi.getConfig();
		ActionModel a = con.find("/studentAction");
		System.out.println(a);
		
		ForwardModel students = a.find("students");
		System.out.println(students);
	}
}

同时利用了dom4j+xpath技术实现的XML建模

运行结果如下:

今天的代码到这里就结束啦,咱们下期见~

今天也要记得微笑呀. 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值