XML建模
XML建模
1.什么是XML建模
- 将XML配置文件中的元素,属性,文本信息 转换成对象的过程叫XML建模。
2.XML建模
- 根据XML配置文件元素节点创建元素,节点,实体类
ConfigModel ActionModel ForwardModel - 利用dom4j+xpath技术实现XML建模ConfigModelFactory
建模思路
1.XML文件 config.xml
2.根据XML中元素节点 情况(DTD)未定义 ConfigModel , ActionModel , ForwardModel 对象模型。
- config节点下有多个节点,无节点属性
- action节点下有多个forward节点,有节点属性
- forward下无节点,有节点属性
3.使用Map集合存放子节点元素,其中Key为子节点的唯一属性,Value为整个子节点对象。
4.利用工程模式+dom4j+xpath解析XML配置文件
//解析XML配置文件
package com.zking.xmlModel.action;
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 {
private static final String DEFAULT_PATH = "/config.xml";
public ConfigModelFactory() {
}
public static ConfigModel createConfigModel(String path) {
// 创建ConfigModel对象
ConfigModel configModel = new ConfigModel();
ActionModel actionModel = new ActionModel();
ForwardModel forwardModel = new ForwardModel();
// 定义属性
String actionPath = null;
String actionType = null;
String forwardName = null;
String forwardPath = null;
String forwardRedirect = null;
// TODO
try {
// 1.获取ConfigModel文件的输入流
InputStream is = ConfigModelFactory.class.getResourceAsStream(path);
// 2.创建SAXReader对象
SAXReader saxReader = new SAXReader();
// 3.加载XML的文件的输入流,并转换成Document对象
Document doc = saxReader.read(is);
// 4.循环遍历①selectNodes②selectSingleNode
// xpath:/定位路径 @获取属性
List<Node> actionNodes = doc.selectNodes("/config/action");
for (Node actionNode : actionNodes) {
// 将节点对象转换成ELement元素对象
Element actionElem = (Element) actionNode;
// 读取ActionModel实体类对象
actionPath = actionElem.attributeValue("path");
actionType = actionElem.attributeValue("type");
// 创建ActionModel实体类对象
actionModel.setPath(actionPath);
actionModel.setType(actionType);
// 遍历forward节点
List<Node> forwardNodes = actionElem.selectNodes("forward");
for (Node forwardNode : forwardNodes) {
Element forwardELem = (Element) forwardNode;
forwardName = forwardELem.attributeValue("name");
forwardPath = forwardELem.attributeValue("path");
forwardRedirect = forwardELem.attributeValue("redirect");
// 创建forwardModel实体类对象
forwardModel.setName(forwardName);
forwardModel.setPath(forwardPath);
forwardModel.setRedirect(Boolean.parseBoolean(forwardRedirect));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
} catch (DocumentException e) {
e.printStackTrace();
}
return configModel;
}
public static ConfigModel createConfigModel() {
return createConfigModel(DEFAULT_PATH);
}
public static void main(String[] args) {
ConfigModel configModel = ConfigModelFactory.createConfigModel();
System.out.println(configModel);
//提取节点数据
ActionModel action = configModel.get("/regAction");
//System.out.println(action);
System.out.println("action:"+action.getPath());
System.out.println("action:"+action.getType());
/*ForwardModel forwardModel = action.get("success");
System.out.println("forward:"+forwardModel.getName());
System.out.println("forward:"+forwardModel.getPath());
System.out.println("forward:"+forwardModel.getRedirect());*/
}
}
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>