1、什么叫XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模。
2、XML建模
1)根据XML配置文件元素节点创建元素节点实体类(ConfigModel、ActionModel、ForwardModel)
2)利用dom4j+xpath技术实现XML建模(ConfigModelFactory)
DTD约束:由XML的根节点往里建立约束
XML建模:由最里层节点往根节点进行建模,一个元素节点代表一个实体类
3、思路
1)xml文件config.xml
config.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>
2)根据XML中元素节点情况(DTD)来定义ConfigModel、ActionModel、ForwardModel对象模型
ForwardModel类:forward下无子节点,有节点属性
package com.zking.xmlModel.entity;
import java.io.Serializable;
/**
* forward
* 对应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 + "]";
}
}
ActionModel类:节点下有多个forward节点(0~N个),有节点属性
package com.zking.xmlModel.entity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* config节点
* 对应config.xml中config节点所建立的建模实体类
* <config>——>ConfigModel
* 包含关系:ConfigModel->ActionModel->FoewardModel(0~N)
* @author zjjt
*
*/
public class ConfigModel implements Serializable {
/**
* key:代表action节点本身的path属性,唯一
* value:代表action节点本身
*/
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类:有多个action节点,无节点属性,根节点
package com.zking.xmlModel.entity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* action节点
* 对应config.xml中action节点所建立的建模实体类
* <action>——>ActionModel
* 包含关系:ActionModel->FoewardModel(0~N)
* @author zjjt
*
*/
public class ActionModel implements Serializable {
private String path;
private String type;
/**
* key:代表forword节点本身的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 + "]";
}
}
3)使用Map集合存放子节点元素,其中key为子节点唯一属性,value为整个子节点对象
ActionModel:存在包含关系
/**
* key:代表forword节点本身的name属性,唯一
* value:代表forward节点本身
*/
private Map<String,ForwardModel> forwards=new HashMap<>();
ConfigModel:存在包含关系
/**
* key:代表action节点本身的path属性,唯一
* value:代表action节点本身
*/
private Map<String,ActionModel> actions=new HashMap<>();
4)利用工厂模式+dom4j+xpath配置文件
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解析建模操作
try {
//1.获取文件输入流
InputStream is =ConfigModelFactory.class.getResourceAsStream(path);
// System.out.println(is);
//2.创建SAXReader对象
SAXReader saxReader=new SAXReader();
//3.读取文件输入流并转成Document对象
//注:Document包含整个XML中元素,属性以及文本信息
Document doc=saxReader.read(is);
//4.解析XML
//注意
//1)获取多个节点:selectNodes
//2)获取单个节点:selectSingleNodes
List<Node> actionNodes = doc.selectNodes("/config/action");
//5.循环换遍历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> forwadrNodes = actionElem.selectNodes("forward");
//10.循环遍历forward
for (Node forward : forwadrNodes) {
//11.将forward节点转换成元素节点(<forward>)
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();
}
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属性等于sucess
ForwardModel forwardModel=actionModel.get("success");
System.out.println("name="+forwardModel.getName());
System.out.println("path="+forwardModel.getPath());
System.out.println("redirect="+forwardModel.isRedirect());
}
}
需要的jar包:
总结:
灵活应用嵌套,最后调用方法!!!