一.什么叫XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
二. XML建模
1)根据XML配置文件元素节点创建元素节点实体类
ConfigModel、ActionModel、ForwardModel
2)利用dom4j+xpath技术实现XML建模
ConfigModelFactory
2.0 config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- config标签:可以包含0~N个action标签 -->
<!DOCTYPE config [
<!ELEMENT config (action*)>
<!ELEMENT action (forward*)>
<!ATTLIST action
path CDATA "/"
type CDATA #REQUIRED
>
<!ATTLIST forward
name CDATA #REQUIRED
path CDATA "/"
redirect (false|true) "false"
>
]>
<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>
2.1 ActionModel类
package com.zking.XMLModel.entity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* ActionModel实体类对应config.xml中action节点
*
* @author Administrator
*
*/
public class ActionModel implements Serializable {
private String path;
private String type;
//key:代表forward节点的name属性,唯一
//value:代表整合forward对象所对应的建模实体类ForwardModel
private Map<String,ForwardModel> forwards=new HashMap<>();
/**
* 取值方法
* @param name
* @return
*/
public ForwardModel get(String name) {
return forwards.get(name);
}
/**
* 存值方法
* @param forward 根据forward的name属性作为key,forward对象
*/
public void push(ForwardModel forward) {
forwards.put(forward.getName(), forward);
}
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;
}
}
2.2 ConfigModel类
package com.zking.XMLModel.entity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* ConfigModel实体类对应config.xml中config节点
* @author Administrator
*
*/
public class ConfigModel implements Serializable{
//key:代表action节点的path属性,唯一
//value:代表action节点所对应的建模实体类ActionModel
private Map<String,ActionModel> actions=new HashMap<>();
/**
* 取值方法
* @param path
* @return
*/
public ActionModel get(String path) {
return actions.get(path);
}
/**
* 存值方法
* @param action 根据action中的path属性作为key,以整个action节点作为value值
*/
public void push(ActionModel action) {
actions.put(action.getPath(), action);
}
}
2.3 ForWordModel类
package com.zking.XMLModel.entity;
import java.io.Serializable;
/**
* ForwartModel实体类对应config.xml中forward标签
*
* @author Administrator
*
*/
public class ForwardModel implements Serializable {
// name属性对应config.xml中forward节点的name属性
private String name;
// name属性对应config.xml中forward节点的path属性
private String path;
// name属性对应config.xml中forward节点的redirect属性
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;
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
2.4 ConfigModelFactory类
package com.zking.XMLModel.util;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
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";
public 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;
try {
// 1.获取文件输入流
InputStream is = ConfigModelFactory.class.getResourceAsStream(path);
// 2.创建SAXReader对象
SAXReader saxReader = new SAXReader();
// 3.读取文件 输入流并转换成Document对象
Document doc = saxReader.read(is);
// 4.解析XML文件
// 获取多个节点:selectNodes
// 获取单个节点:selectSingleNode
// xpath语法: / 代表定位路径 @ 代表获取属性
List<Node> actionNodes = doc.selectNodes("/config/action");
// 循环遍历action节点
for (Node action : actionNodes) {
// 5.将action节点转换成元素节点
Element actionElem = (Element) action;
// 6.逐一获取action节点中的属性(path和type)
String actionpath = actionElem.attributeValue("path");
String actionType = actionElem.attributeValue("type");
// 7.初始化ActionModel并完成建模赋值操作
actionModel = new ActionModel();
actionModel.setPath(actionpath);
actionModel.setType(actionType);
// 8.获取action节点下的forward节点(0-N个)
List<Node> forwardNodes = actionElem.selectNodes("forward");
// 9.循环遍历所有的forward节点
for (Node forward : forwardNodes) {
// 10.将forward节点转换成元素节点
Element forwardElem = (Element) forward;
// 11.获取forward节点所有属性(name.path和redirect)
String forwardName = forwardElem.attributeValue("name");
String forwardPath = forwardElem.attributeValue("path");
String forwardRedirect = forwardElem.attributeValue("redirect");
// 12.创建forwardModel建模对象并完成赋值操作
forwardModel = new ForwardModel();
forwardModel.setName(forwardName);
forwardModel.setPath(forwardPath);
forwardModel.setRedirect(Boolean.parseBoolean(forwardRedirect));
// 13.action节点中包含0-N个forward节点
actionModel.push(forwardModel);
}
// 14.config节点中包含0-N个action节点
configModel.push(actionModel);
}
} catch (DocumentException e) {
e.printStackTrace();
}
return configModel;
}
public static void main(String[] args) {
ConfigModel createConfigModel = ConfigModelFactory.createConfigModel();
ActionModel actionModel = createConfigModel.get("/regAction");
System.out.println("action节点path属性:" + actionModel.getPath());
System.out.println("action节点type属性:" + actionModel.getType());
System.out.println("-----------------------------------------");
ForwardModel forwardModel = actionModel.get("failed");
System.out.println("forward节点name属性:" + forwardModel.getName());
System.out.println("forward节点path属性:" + forwardModel.getPath());
System.out.println("forward节点redirect属性:" + forwardModel.isRedirect());
}
}
DTD约束:由XML的根节点往里建立约束
XML建模:由最里层节点往根节点进行建模,一个元素节点代表一个实体类思路:
1)xml文件config.xml2)根据XML中元素节点情况(DTD)来定义ConfigModel、ActionModel、ForwardModel对象模型
A.config节点下有多个子action节点,无节点属性
B.action节点下有多个子forward节点,有节点属性
C.forward下无子节点,有节点属性3)使用Map集合存放子节点元素,其中key为子节点唯一属性,value为整个子节点对象
4)利用工厂模式+dom4j+xpath解析Xml配置文件