目录
需导入包:dom4j-1.6.1.jar jaxen-1.1-beta-6.jar
2)根据XML中元素节点情况(DTD)来定义ConfigModel、ActionModel、ForwardModel对象模型
3)使用Map集合存放子节点元素,其中key为子节点唯一属性,value为整个子节点对象
1.什么叫XML建模
1.1)将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
1.2)思维导图
2. 案例:XML建模
1)根据XML配置文件元素节点创建元素节点实体类
ConfigModel、ActionModel、ForwardModel
2)利用dom4j+xpath技术实现XML建模
ConfigModelFactory
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>
3个实体类
import java.io.Serializable;
/**
* xml建模 forward标签对应的对象模型类
* @author Administrator
*
*/
public class ForwardModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5368245028680071006L;
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 getRedirect() {
return redirect;
}
public void setRedirect(Boolean redirect) {
this.redirect = redirect;
}
@Override
public String toString() {
return "ForwordModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* xml建模 action标签对应的对象模型类
* @author Administrator
*
*/
public class ActionModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6490183796991304385L;
private String path;
private String type;
//存放action节点下的forward对象模型的集合, 以forward的name属性值为键,forwardmodel对象为值
private Map<String, ForwardModel> Forwords =
new HashMap<String, ForwardModel>();
public ActionModel() {
}
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 Map<String, ForwardModel> getForwords() {
return Forwords;
}
public void setForwords(Map<String, ForwardModel> forwords) {
Forwords = forwords;
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + "]";
}
/**
* 将forwardmodel对象放入map集合的方法
* @param forwordmodel
*/
public void push(ForwardModel forwordmodel) {
this.Forwords.put(forwordmodel.getName(), forwordmodel);
}
/**
* 通过forward的name值从map集合中取出对应的forwardmodel对象
* @param name
* @return
*/
public ForwardModel getForwordModel(String name) {
return this.Forwords.get(name);
}
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* xml建模 config标签对应的对象模型类
* @author Administrator
*
*/
public class ConfigModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4636993202614324739L;
//存放config节点下的action对象模型的集合, 以action的path属性值为键,ActionModel对象为值
private Map<String, ActionModel> actions =
new HashMap<String, ActionModel>();
public ConfigModel() {
super();
}
public Map<String, ActionModel> getActions() {
return actions;
}
public void setActions(Map<String, ActionModel> actions) {
this.actions = actions;
}
@Override
public String toString() {
return "ConfigModel [actions=" + actions + "]";
}
/**
* 将ActionModel对象放入map集合的方法
* @param ActionModel
*/
public void push(ActionModel actionModel) {
this.actions.put(actionModel.getPath(), actionModel);
}
/**
* 通过forward的name值从map集合中取出对应的forwardmodel对象
* @param name
* @return
*/
public ActionModel getActionModel(String path) {
return this.actions.get(path);
}
}
思路:
1)xml文件config.xml
2)根据XML中元素节点情况(DTD)来定义ConfigModel、ActionModel、ForwardModel对象模型
A.config节点下有多个子action节点,无节点属性
B.action节点下有多个子forward节点,有节点属性
C.forward下无子节点,有节点属性
3)使用Map集合存放子节点元素,其中key为子节点唯一属性,value为整个子节点对象
4)利用工厂模式+dom4j+xpath解析Xml配置文件
/**
* 读取配置文件,将配置信息储存到ConfigModel中
* @author Administrator
*
*/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.xml.entity.ActionModel;
import com.zking.xml.entity.ConfigModel;
import com.zking.xml.entity.ForwardModel;public class ConfigModelFactory {
public static ConfigModel creatConfigModel(String path) {
//1.获取io流
InputStream is = ConfigModelFactory.class.getResourceAsStream("/config.xml");
//定义需要用的变量
ActionModel actionModel = null;
String actionpath = null;
String actiontype = null;
String forwordName = null;
String forwordPath = null;
String forwordRedirect = null;
ForwardModel forwardModel = null;
ConfigModel configModel = new ConfigModel();
//2.创建xml读取工具类SaxReader对象
SAXReader sr = new SAXReader();
//3.读取config.xml文件,获取document对象
try {
Document doc = sr.read(is);
//4.通过document对象中的selectNodes()方法找到action节点
List<Node> nodes = doc.selectNodes("config/action");
//5.遍历action节点,获取action节点中的path和type属性值
for (Node actionNode : nodes) {
//将节点对象强转为元素对象
Element el = (Element)actionNode;
//获取属性值
actionpath = el.attributeValue("path");
actiontype = el.attributeValue("type");
//实例化ActionModel对象
actionModel = new ActionModel();
//设置到actionModel对象中
actionModel.setPath(actionpath);
actionModel.setType(actiontype);
//6.找到该action节点下的forward节点
List<Node> selectNodes = actionNode.selectNodes("forward");
//7.遍历forward节点,
for (Node forwardNode : selectNodes) {
//将节点对象强转为元素对象
Element forwordEl = (Element)forwardNode;
//获取forward节点中的name,path,redirect属性值,并设置forwardModel对象中
forwordName = forwordEl.attributeValue("name");
forwordPath = forwordEl.attributeValue("path");
forwordRedirect = forwordEl.attributeValue("redirect");
//实例化ForwardModel对象
forwardModel = new ForwardModel();
forwardModel.setName(forwordName);
forwardModel.setPath(forwordPath);
forwardModel.setRedirect(Boolean.parseBoolean(forwordRedirect));
//8.将forwardModel对象放入到actionModel对象的map集合中,以name为键,forwardModel对象为值
actionModel.push(forwardModel);
//9.将actionModel对象放入到ConfigModel对象中,完成建模
}
configModel.push(actionModel);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return configModel;
}
public static void main(String[] args) {
ConfigModel creatConfigModel = ConfigModelFactory.creatConfigModel("/config.xml");
ActionModel actionModel = creatConfigModel.getActionModel("/regAction");
System.out.println(actionModel.getType());
ForwardModel forwordModel = actionModel.getForwordModel("success");
System.out.println(forwordModel.getPath());
}
}