XML的建模

 思维导图:

 

XML建模就是以面向对象的编程思想,将指定的xml字符串当作对象来操作

 需要导入的包:

Ⅰ:dom4j-1.6.1.jar

Ⅱ:jaxen-1.1-beta-6.jar 

xml的文件的结构(导图):

 先是forword

package com.LSZ;

public class FowardModel {

	private String name;
	private String path;
	private String redirect;

	public FowardModel() {
		// TODO Auto-generated constructor stub
	}

	public FowardModel(String name, String path, String redirect) {
		super();
		this.name = name;
		this.path = path;
		this.redirect = redirect;
	}

	@Override
	public String toString() {
		return "FowardModel [name=" + name + ", path=" + path + ", 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 String getRedirect() {
		return redirect;
	}

	public void setRedirect(String redirect) {
		this.redirect = redirect;
	}
}

 actionmodel

package com.LSZ;

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

public class ActionModel {

	private String path;
	private String type;
	private Map<String, FowardModel> map=new HashMap<String, FowardModel>();
	public ActionModel() {
		// TODO Auto-generated constructor stub
	}

	public ActionModel(String path, String type) {
		super();
		this.path = path;
		this.type = type;
	}

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

	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(FowardModel a) {
		map.put(a.getPath(), a);
	}
	
	
	
	public FowardModel pop(String name) {
		return map.get(name);
	}
}

configmodel

 

package com.LSZ;

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

public class ConfigModel {

		Map<String, ActionModel> map=new HashMap<>();
		
		public void push(ActionModel a) {
			map.put(a.getPath(), a);
		}
		
		
		
		public ActionModel pop(String path) {
			return map.get(path);
		}
}

建模

package com.LSZ;

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;

public class ConfigModelFactory {

	public ConfigModel buibl() throws Exception {
		return buibl("config.xml");
	}

	private ConfigModel buibl(String paths) throws Exception {
		InputStream in = ConfigModelFactory.class.getResourceAsStream(paths);
		// 得到解析器
		SAXReader sr = new SAXReader();
		Document doc = sr.read(in);
		ConfigModel con = new ConfigModel();
		// 拿到对应的节点对象
		List<Element> action = doc.selectNodes("/config/action");
		for (Element element : action) {
			ActionModel a = new ActionModel();
			a.setPath(element.attributeValue("path"));
			a.setType(element.attributeValue("type"));
			con.push(a);
			List<Element> forward = element.selectNodes("forward");
			for (Element element2 : forward) {
				FowardModel f = new FowardModel();
				f.setName(element2.attributeValue("name"));
				f.setPath(element2.attributeValue("path"));
				f.setRedirect(element2.attributeValue("redirect"));
				a.push(f);
			}

		}

		return con;
	}

}

测试类

package com.LSZ;

public class test {
	public static void main(String[] args) throws Exception {
		ConfigModel c = new ConfigModelFactory().buibl();
		ActionModel pop = c.pop("/loginAction");
		System.out.println(pop.getType());
	}
}

输出结果

 

web.xml 

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
  	<servlet-name>jrebelServlet</servlet-name>
  	<servlet-class>com.zking.xml.JrebelServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>jrebelServlet</servlet-name>
  	<url-pattern>/jrebelServlet</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>jrebelServlet2</servlet-name>
  	<servlet-class>com.zking.xml.JrebelServlet2</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>jrebelServlet2</servlet-name>
  	<url-pattern>/jrebelServlet2</url-pattern>
  	<url-pattern>/jrebelServlet3</url-pattern>
  </servlet-mapping>
</web-app>

 1.ServletModel类

package com.LSZ;

public class ServletModel extends WebAppModel{
	private String servletname;
	private String servletcalss;

	public String getServletname() {
		return servletname;
	}

	public void setServletname(String servletname) {
		this.servletname = servletname;
	}

	public String getServletcalss() {
		return servletcalss;
	}

	public void setServletcalss(String servletcalss) {
		this.servletcalss = servletcalss;
	}

	public ServletModel(String servletname, String servletcalss) {
		super();
		this.servletname = servletname;
		this.servletcalss = servletcalss;
	}

	@Override
	public String toString() {
		return "ServletDodel [servletname=" + servletname + ", servletcalss=" + servletcalss + "]";
	}

	public ServletModel() {
		// TODO Auto-generated constructor stub
	}

}

2.ServletMappingModel 类

 

package com.LSZ;

public class ServletMappingModel extends WebAppModel{
	private String servletname;
	private String urlpattern;

	public String getServletname() {
		return servletname;
	}

	public void setServletname(String servletname) {
		this.servletname = servletname;
	}

	public String getUrlpattern() {
		return urlpattern;
	}

	public void setUrlpattern(String urlpattern) {
		this.urlpattern = urlpattern;
	}

	@Override
	public String toString() {
		return "ServletMappingDodel [servletname=" + servletname + ", urlpattern=" + urlpattern + "]";
	}

	public ServletMappingModel(String servletname, String urlpattern) {
		super();
		this.servletname = servletname;
		this.urlpattern = urlpattern;
	}

	public ServletMappingModel() {
		// TODO Auto-generated constructor stub
	}

}

 3:WebappModel  类 

 

package com.LSZ;

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


public class WebAppModel {

	Map<String, WebAppModel> map = new HashMap<>();
 
	public void push(WebAppModel w) {
		if (w instanceof ServletModel) {// 判断w是不是ServletDodel的实类
 
			map.put(((ServletModel) w).getServletname(), (ServletModel) w);
		}
		if (w instanceof ServletMappingModel) {// 判断w是不是ServletMappingDodel的实类
			map.put(((ServletMappingModel) w).getUrlpattern(), (ServletMappingModel) w);
		}
 
	}
 
	public WebAppModel pop(String name) {
		return map.get(name);
	}


}

4:建模

 

package com.LSZ;

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;

public class WebAppModelFactory {
	public WebAppModel biblu() throws DocumentException {
		return biblu("web.xml");
	}

	private WebAppModel biblu(String path) throws DocumentException {
		// 得到文件输入流
		InputStream in = ServletMappingModel.class.getResourceAsStream(path);
		// 解析器
		SAXReader sr = new SAXReader();
		Document dom = sr.read(in);
		// 定义WebappModel 容器
		WebAppModel w = new WebAppModel();
		List<Element> servletent = dom.selectNodes("/web-app/servlet");
		for (Element element : servletent) {
			// 赋值
			ServletModel ser = new ServletModel();
			ser.setServletcalss(element.selectSingleNode("servlet-class").getText());
			ser.setServletname(element.selectSingleNode("servlet-name").getText());
			w.push(ser);
		}
		List<Element> servletmapping = dom.selectNodes("/web-app/servlet-mapping");
		for (Element element : servletmapping) {
			// 赋值
			ServletMappingModel sermin = new ServletMappingModel();
			sermin.setServletname(element.selectSingleNode("servlet-name").getText());
			sermin.setUrlpattern(element.selectSingleNode("url-pattern").getText());
			w.push(sermin);
		}
		return w;
	}

}

 5:测试类

package com.LSZ;

import org.dom4j.DocumentException;

public class TRY {
	public static void main(String[] args) throws DocumentException {
		WebAppModel w = new WebAppModelFactory().biblu();
		/**
		 * 拿到ServletMappingDodel 类 再通过ServletMappingDodel类 的 servletname属性
		 * 拿ServletDodel类的servletcalss 属性
		 */

		ServletMappingModel pop = (ServletMappingModel) w.pop("/jrebelServlet2");
		ServletModel pop2 = (ServletModel) w.pop(pop.getServletname());
		System.out.println(pop2.getServletcalss());
	}

}

 运行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值