目录
一、什么叫XML建模
二、XML建模
三、案例
一、什么叫XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
二、XML建模
(1)根据XML配置文件元素节点创建元素节点实体类
(2)利用dom4j+xpath技术实现XML建模
(1)根据XML配置文件元素节点创建元素节点实体类
(2)利用dom4j+xpath技术实现XML建模
三、案例
需要的jar包:dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
config.xml
]>
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 forwardMap = new HashMap();
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 actionMap = new HashMap();
public Map getActionMap() {
return actionMap;
}
public void setActionMap(Map actionMap) {
this.actionMap = actionMap;
}
public ConfigModel() {
// TODO Auto-generated constructor stub
}
public ConfigModel(Map 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 actionNode = doc.selectNodes("/config/action");
for (Node acNode : actionNode) {
Element acEl = (Element)acNode;
am = new ActionModel(acEl.attributeValue("path"), acEl.attributeValue("type"));
List 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());
}
}
需要的jar包:dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
config.xml
]>
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 forwardMap = new HashMap();
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 actionMap = new HashMap();
public Map getActionMap() {
return actionMap;
}
public void setActionMap(Map actionMap) {
this.actionMap = actionMap;
}
public ConfigModel() {
// TODO Auto-generated constructor stub
}
public ConfigModel(Map 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 actionNode = doc.selectNodes("/config/action");
for (Node acNode : actionNode) {
Element acEl = (Element)acNode;
am = new ActionModel(acEl.attributeValue("path"), acEl.attributeValue("type"));
List 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());
}
}



