目录
2.1简单来说就是将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
3.1根据XML配置文件元素节点创建元素节点实体类 ConfigModel、ActionModel、ForwardModel
3.2利用dom4j+xpath技术实现XML建模 ConfigModelFactory
一、xml建模思维导图
二、什么叫XML建模以及项目结构和需要jar包
2.1简单来说就是将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
2.2jar包需要两个:
2.3项目结构如下截图所示:
三、 XML建模实例:
3.1根据XML配置文件元素节点创建元素节点实体类
ConfigModel、ActionModel、ForwardModel
3.2利用dom4j+xpath技术实现XML建模
ConfigModelFactory3.4所有代码如下
3.4.1config.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.4.2ForwardModel 实体类代码如下:
package com.zking.xml.entity; import java.io.Serializable; /** * xml建模:forward标签对应的对象模型类 * @author Administrator * */ public class ForwardModel implements Serializable{ private static final long serialVersionUID = 6235025382426570108L; private String name; private String path; private boolean redirect; public ForwardModel() { // TODO Auto-generated constructor stub } 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(String name, String path, boolean redirect) { this.name = name; this.path = path; this.redirect = redirect; } }
3.4.3ActionModel 实体类代码如下:
package com.zking.xml.entity; 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> forwards = new HashMap<String, ForwardModel>(); public Map<String, ForwardModel> getForwards() { return forwards; } public void setForwards(Map<String, ForwardModel> forwards) { this.forwards = forwards; } public ActionModel() { // TODO Auto-generated constructor stub } 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(String path, String type) { this.path = path; this.type = type; } /** * 将forward对象放入map集合中的方法 * @param actionmodel */ public void push(ForwardModel forwarmodel) { this.forwards.put(forwarmodel.getName(), forwarmodel); } /** * 通过forward的name从map集合中取出对应的ForwardModel对象 * @param name * @return */ public ForwardModel forwardmodel(String name) { return this.forwards.get(name); } @Override public String toString() { return "ActionModel [path=" + path + ", type=" + type + "]"; } }
3.4.4ConfigModel 实体类代码如下:
package com.zking.xml.entity; 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 Map<String, ActionModel> getActions() { return actions; } public void setActions(Map<String, ActionModel> actions) { this.actions = actions; } /** * 将action对象放入map集合中的方法 * @param actionmodel */ public void push(ActionModel actionmodel) { this.actions.put(actionmodel.getPath(), actionmodel); } /** * 通过action的path从map集合中取出对应的ActionModel对象 * @param path * @return */ public ActionModel actionmodel(String path) { return this.actions.get(path); } public ConfigModel() { // TODO Auto-generated constructor stub } public ConfigModel(Map<String, ActionModel> actions) { this.actions = actions; } @Override public String toString() { return "ConfigModel [actions=" + actions + "]"; } }
3.4.5ConfigModelFactory 代码如下:
package com.zking.xml.utils; 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; /** * 读取配置文件,将信息存储到configmodel对象中 * @author Administrator * */ public class ConfigModelFactory { public static final String DEFAULT_PATH = "/config.xml"; /** * 私有的构造方法,不能在外部被实例化 */ private ConfigModelFactory() { } /** * 读取配置文件,将信息存储到configmodel对象中 * @return */ public static ConfigModel createConfigModel(String path) { //1.获取io流 InputStream is = ConfigModelFactory.class.getResourceAsStream(path); //2.创建xml读取工具类SaxReader SAXReader sr = new SAXReader(); ActionModel ACTIONMODEL = null; ConfigModel CONFIGMODEL = new ConfigModel(); ForwardModel FORWARDMODEL = null; String ACTIONPATH = null; String ACTIONTYPE = null; String FORWARDNAME = null; String FORWARDPATH = null; String FORWARDREDIRECT = null; try { //3.读取config.xml文件,获得document对象 Document doc = sr.read(is); //4.找到action节点 List<Node> actionnode = doc.selectNodes("/config/action"); //5.遍历action节点获取path和type属性值 for (Node node : actionnode) { //节点对象没有属性 获取属性 1.将node转换成元素element Element actionel = (Element)node; //获取属性值 ACTIONPATH = actionel.attributeValue("path"); ACTIONTYPE = actionel.attributeValue("type"); //实例化ActionModel对象 ACTIONMODEL = new ActionModel(); //将属性值放到ActionModel对象中 ACTIONMODEL.setPath(ACTIONPATH); ACTIONMODEL.setType(ACTIONTYPE); //找到该action节点下的forward节点 List<Node> forwardnode = node.selectNodes("/config/action/forward"); //遍历该节点 for (Node node2 : forwardnode) { //节点对象没有属性 获取属性 1.将node转换成元素element Element forwardel = (Element)node2; //获取属性值 FORWARDNAME = forwardel.attributeValue("name"); FORWARDPATH = forwardel.attributeValue("path"); FORWARDREDIRECT = forwardel.attributeValue("redirect"); //实例化FORWARDMODEL对象 FORWARDMODEL = new ForwardModel(); //将属性值放到FORWARDMODEL对象中 FORWARDMODEL.setName(FORWARDNAME); FORWARDMODEL.setPath(FORWARDPATH); FORWARDMODEL.setRedirect(Boolean.parseBoolean(FORWARDREDIRECT)); ACTIONMODEL.push(FORWARDMODEL); } CONFIGMODEL.push(ACTIONMODEL); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return CONFIGMODEL; } public static ConfigModel createConfigModel() { return createConfigModel(DEFAULT_PATH); } public static void main(String[] args) { ConfigModel a = createConfigModel(); ActionModel actionmodel = a.actionmodel("/regAction"); System.out.println(actionmodel.getPath()); ForwardModel forwardmodel = actionmodel.forwardmodel("failed"); System.out.println(forwardmodel.getName()); } }
3.4.6控制台打印的效果截图: