目录
一、什么叫XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
二、XML建模
(1)根据XML配置文件元素节点创建元素节点实体类
(2)利用dom4j+xpath技术实现XML建模
三、案例
需要的jar包:dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
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>
ForwardModel.java
package com.zking.entity; import java.io.Serializable; public class ForwardModel implements Serializable{ /** * */ private static final long serialVersionUID = 3557052377484390295L; 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; } public ForwardModel() { // TODO Auto-generated constructor stub } public ForwardModel(String name, String path, Boolean redirect) { super(); this.name = name; this.path = path; this.redirect = redirect; } @Override public String toString() { return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]"; } }
ActionModel.java
package com.zking.entity; import java.io.Serializable; import java.util.HashMap; import java.util.Map; public class ActionModel implements Serializable{ /** * */ private static final long serialVersionUID = -7058655227463933636L; private String path; private String type; private Map<String, ForwardModel> forwardMap = new HashMap<String, ForwardModel>(); 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() { // TODO Auto-generated constructor stub } public ActionModel(String path, String type) { super(); this.path = path; this.type = type; } public void push(ForwardModel fm) { this.forwardMap.put(fm.getName(), fm); } public ForwardModel getFM(String name) { return this.forwardMap.get(name); } @Override public String toString() { return "ActionModel [path=" + path + ", type=" + type + ", forwardMap=" + forwardMap + "]"; } }
ConfigModel.java
package com.zking.entity; import java.io.Serializable; import java.util.HashMap; import java.util.Map; public class ConfigModel implements Serializable{ /** * */ private static final long serialVersionUID = 7753991598150098506L; private Map<String, ActionModel> actionMap = new HashMap<String, ActionModel>(); public Map<String, ActionModel> getActionMap() { return actionMap; } public void setActionMap(Map<String, ActionModel> actionMap) { this.actionMap = actionMap; } public ConfigModel() { // TODO Auto-generated constructor stub } public ConfigModel(Map<String, ActionModel> actionMap) { this.actionMap = actionMap; } public void push(ActionModel am) { this.actionMap.put(am.getPath(), am); } public ActionModel getAM(String path) { return this.actionMap.get(path); } }
ConfigModelFactory.java
package com.zking.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.entity.ActionModel; import com.zking.entity.ConfigModel; import com.zking.entity.ForwardModel; public class ConfigModelFactory { private static final String DEFAULT_PATH = "/config.xml"; private ConfigModelFactory() { } public static ConfigModel createConfigModel(String path) { InputStream is = ConfigModelFactory.class.getResourceAsStream(path); SAXReader sr = new SAXReader(); ActionModel am = null; ForwardModel fm = null; ConfigModel cm = new ConfigModel(); try { Document doc = sr.read(is); List<Node> actionNode = doc.selectNodes("/config/action"); for (Node acNode : actionNode) { Element acEl = (Element)acNode; am = new ActionModel(acEl.attributeValue("path"), acEl.attributeValue("type")); List<Node> forwardNode = acNode.selectNodes("forward"); for (Node forNode : forwardNode) { Element forEl = (Element)forNode; fm = new ForwardModel( forEl.attributeValue("name"), forEl.attributeValue("path"), Boolean.parseBoolean(forEl.attributeValue("redirect")) ); am.push(fm); } cm.push(am); } } catch (Exception e) { e.printStackTrace(); } return cm; } public static ConfigModel createConfigModel() { return createConfigModel(DEFAULT_PATH); } public static void main(String[] args) { ConfigModel cm = ConfigModelFactory.createConfigModel(); ActionModel am = cm.getAM("/regAction"); ForwardModel fm = am.getFM("failed"); System.out.println(fm.getPath()); } }