J2EE .XML建模

本文详细介绍了XML建模的概念,通过实例展示了如何根据XML配置文件创建元素节点实体类,并利用DOM4J和XPath技术进行XML建模。文中提供了ConfigModel、ActionModel和ForwardModel三个实体类的代码实现,并通过ConfigModelFactory实现了XML解析建模操作。此外,还展示了如何从XML配置文件中获取特定节点信息的示例。
摘要由CSDN通过智能技术生成

目录

思维导图

1.什么叫XML建模

2. XML建模

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

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

3、xml建模的代码完成

1、xml建模的初步代码

2、Factory工厂


思维导图

1.什么叫XML建模

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

2. XML建模

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

     ConfigModel、ActionModel、ForwardModel


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


   ConfigModelFactory
   DTD约束:由XML的根节点往里建立约束
   XML建模:由最里层节点往根节点进行建模,一个元素节点代表一个实体类

 

3、xml建模的代码完成

1、xml建模的初步代码

下面是一个xml解析的代码:

<?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:字符串,非空
	-->
	<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>

一个标签就是一个对象,那么在这个代码中,就存在三个对象Config、action、forward。
一个类由两部分(属性和方法)组成,那我们就通过这两部分对这三个对象进行分析:

Config对象——>ConfigModelg类
属性:没有属性
行为:新增Action对象的行为,通过action path属性查找Action对象的行为。
 
ActionModel类
属性:path属性、type属性
行为:新增Forward对象的行为,通过Forward对象name属性查找对应的Forward对象的行为
 
ForwardModel类
属性:name属性、path属性、redirect属性
行为:没有

分析好之后,开始准备代码。
注意顺序由里到外,由forward写到config。

建包顺序也就是ForwardModel——>ActionModel——>ConfigModel
ForwardModel里面的代码是:

package com.zking.xmlmodel.entity;

import java.io.Serializable;
/**
 * 对应config.xml中forward节点所建立的建模实体类
 * <forward> ->ForwardModel
 * @author zjjt
 *
 */
public class ForwardModel implements Serializable {
	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;
	}
	public ForwardModel() {
		super();
	}
	@Override
	public String toString() {
		return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
	}
	
}


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

ActionModel里面的代码是:

package com.zking.xmlmodel.entity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
 * 对应config.xml中action节点所建立的建模实体类
 * <action> ->ActionModel
 * 包含关系:ActionModel -> ForwardModel (0~N)
 * @author zjjt
 *
 */
public class ActionModel implements Serializable {

	private String path;
	private String type;
	//Key:代表forward节点中name属性,唯一
	//Value:代表forward节点本身
	private Map<String,ForwardModel> forwards=new HashMap<>();
	public void push(ForwardModel forward) {
		forwards.put(forward.getName(), forward);
	}
	public ForwardModel get(String name) {
		return forwards.get(name);
	}
	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 ActionModel() {
		super();
	}
	@Override
	public String toString() {
		return "ActionModel [path=" + path + ", type=" + type + "]";
	}
	
	
	
}


ConfigModel里面的代码是:

package com.zking.xmlmodel.entity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
 * 对应config.xml中config节点所建立的建模实体类
 * <config> ->ConfigModel
 * 包含关系:ConfigModel -> ActionModel -> ForwardModel (0~N)
 * @author zjjt
 *
 */
public class ConfigModel implements Serializable {
		//Key:代表forward节点中name属性,唯一
		//Value:代表forward节点本身
	private Map<String,ActionModel> actions=new HashMap<>();
	public void push(ActionModel action) {
		actions.put(action.getPath(), action);
	}
	public ActionModel get(String path) {
		return actions.get(path);
	}
}

三个都完成之后,就开始撞到ConfigModel里面去,建出ConfigFactory工厂类。

2、Factory工厂

工厂模式解决的问题:将代码封装,提高代码的复用性。
类比汉堡的获取方式(肯德基直接购买,原材料自己制作)

package com.zking.xmlmodel.util;

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

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

import com.zking.xmlmodel.entity.ActionModel;
import com.zking.xmlmodel.entity.ConfigModel;
import com.zking.xmlmodel.entity.ForwardModel;

public class ConfigModelFactory {
	public static final String DEFAULT_PATH="/config.xml";
	
	private ConfigModelFactory() {}
	public static ConfigModel createConfigModel() {
		return createConfigModel(DEFAULT_PATH);
	}
public static ConfigModel createConfigModel(String path) {
		ConfigModel configModel=new ConfigModel();
		ActionModel actionModel=null;
		ForwardModel forwardModel=null;
		//目标:使用dom4j+xpath技术实现XML解析建模操作
		//1.获取文件输入流
		try {
			InputStream is =
					ConfigModelFactory.class.getResourceAsStream(path);
			//2.创建SAXReader对象
			SAXReader saxReader= new SAXReader();
			//3.读取文件输入流并转换成Document对象
			//注:Document包含整个XML中的元素、属性以及文本信息!
			Document doc =saxReader.read(is);
			//4.解析XML
			//注意:
			//1) 获取多个节点:selectNodes
			//2) 获取单个节点:selectSingleNode
			List<Node> actionNodes = doc.selectNodes("/config/action");
			for (Node action : actionNodes) {
				//6、将action节点转换成元素节点(<action>)
				Element actionElem=(Element)action;
				//7、获取action节点中的所有属性信息(path、type)
				String actionPath=actionElem.attributeValue("path");
				String actionType=actionElem.attributeValue("type");
				//8.初始化ActionModel
				actionModel =new ActionModel();
				actionModel.setPath(actionPath);
				actionModel.setType(actionType);
				//9.获取action节点下所有forward节点(0~N)
				List<Node> forwardNodes = 
						actionElem.selectNodes("forward");
				//10.循环遍历forward
				for (Node forward : forwardNodes) {
					Element forwardElem=(Element)forward;
					//12.获取forward节点中的所有属性(name、path以及redirect)
					String forwardName =forwardElem.attributeValue("name");
					String forwardPath =forwardElem.attributeValue("path");
					String forwardRedirect =forwardElem.attributeValue("redirect");
					//13、初始化ForwardModel
					forwardModel=new ForwardModel();
					forwardModel.setName(forwardName);
					forwardModel.setPath(forwardPath);
					forwardModel.setRedirect(Boolean.parseBoolean(forwardRedirect));
					//14.将forwardModel存人到对应的actionModel下
					actionModel.push(forwardModel);
				}
				//15.将actionModel存人到对应的configModel下
				configModel.push(actionModel);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return configModel;
	}
    public static void main(String[] args) {
		ConfigModel configModel = ConfigModelFactory.createConfigModel();
		//要求:获取config节点下的action节点的path属性等于/loginAction的节点对象
		ActionModel actionModel = configModel.get("/loginAction");
		System.out.println("path="+actionModel.getPath());
		System.out.println("type="+actionModel.getType());
		//要求:获取action节点下的forward节点的name属性等于success
		ForwardModel forwardModel = actionModel.get("success");
		System.out.println("name="+forwardModel.getName());
		System.out.println("path="+forwardModel.getPath());
		System.out.println("redirect="+forwardModel.isRedirect());
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梓轩wdw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值