目录
一、什么叫XML建模
二、XML建模
1)根据XML配置文件元素节点创建元素节点实体类
2)利用dom4j+xpath技术实现XML建模
一、什么叫XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
二、XML建模
DTD约束:由XML的根节点往里建立约束
XML建模:由最里层节点往根节点进行建模,一个元素节点代表一个实体类
思路:
1)xml文件config.xml
2)根据XML中元素节点情况(DTD)来定义ConfigModel、ActionModel、ForwardModel对象模型
3)使用Map集合存放子节点元素,其中key为子节点唯一属性,value为整个子节点对象
4)利用工厂模式+dom4j+xpath解析Xml配置文件
1)根据XML配置文件元素节点创建元素节点实体类
ForwardModel
forward下无子节点,有节点属性
/**
* ForwardModel实体类对应config.xml中forward标签所建立的建模实体类
* <forward> --> ForwardModel
* @author Administrator
*
*/
public class ForwardModel implements Serializable {
//name对应config.xml中forward节点的name属性
private String name;
//path对应config.xml中forward节点的的path属性
private String path;
//redirect对应config.xml中forward节点的的redirect属性
private boolean 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;
}
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;
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
ActionModel
action节点下有多个子fowward节点,有节点属性
/**
* ActionModel实体类对应config.xml中action节点所建立的建模实体类
* <action> --> ActionModel
* action节点下是包含0~N个forward
* @author Administrator
*
*/
public class ActionModel implements Serializable {
//path对应config.xml中action节点的path属性
private String path;
//type对应config.xml中action节点的type属性
private String type;
//key:代表forword节点的name属性,唯一
//value:代表整合forward对象所对应的建模实体类ForwardModel
private Map<String, ForwardModel> forwards = new HashMap<String, ForwardModel>();
/**
* 取值方法
* @param name forword节点所对应的name属性,唯一
* @return
*/
public ForwardModel get(String name) {
return forwards.get(name);
}
/**
* 存储方法
* @param forward 根据forword中的name属性作为key,以整个forword节点作为value
*/
public void push(ForwardModel forward) {
forwards.put(forward.getName(), forward);
}
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, Map<String, ForwardModel> forwards) {
super();
this.path = path;
this.type = type;
this.forwards = forwards;
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + ", forwards=" + forwards + "]";
}
}
ConfigModel
config节点下有多个子action节点,无节点属性
/**
* ConfigModel实体类对应config.xml中config节点所建立的建模实体类
* <config> --> ConfigModel
* <action> --> ActionModel
* <forward> --> ForwardModel
* 包含关系:ConfigModel -- ActionModel -- ForwardModel
* 特点:在config节点下包含0~N个action节点,而action节点的path属性唯一
* @author Administrator
*
*/
public class ConfigModel implements Serializable{
//key:代表action节点的path属性,唯一
//value:代表action节点所对应的建模实体类ActionModel
Map<String, ActionModel> actions = new HashMap<String, ActionModel>();
/**
* 取值方法
* @param path action节点所对应的path属性,唯一
* @return
*/
public ActionModel get(String path) {
return actions.get(path);
}
/**
* 存储方法
* @param action 根据action中的path属性作为key,以整个action节点作为value
*/
public void push(ActionModel action) {
actions.put(action.getPath(), action);
}
public ConfigModel() {
// TODO Auto-generated constructor stub
}
public ConfigModel(Map<String, ActionModel> actions) {
super();
this.actions = actions;
}
@Override
public String toString() {
return "ConfigModel [actions=" + actions + "]";
}
}
2)利用dom4j+xpath技术实现XML建模
ConfigModelFactory
public class ConfigModelFactory {
//默认配置文件路径
public static final String DEFAULT_PATTH = "/config.xml";
public ConfigModelFactory() {
// TODO Auto-generated constructor stub
}
public static ConfigModel createConfigModel() {
return createConfigModel(DEFAULT_PATTH);
}
public static ConfigModel createConfigModel(String path) {
//使用demo4j+xpath技术完成XML解析建模操作
ConfigModel configModel = new ConfigModel();
ActionModel actionModel = null;
ForwardModel forwardModel = null;
try {
//获取文件输入流
InputStream is =
ConfigModelFactory.class.getResourceAsStream(path);
//创建SAXReader对象
SAXReader saxr = new SAXReader();
//读取文件输入流并转换成Document对象
Document docu = saxr.read(is);
//解析xml文件
//1)selectSingleNode:获取单个节点
//2)selectNodes:获取多个节点
//3) xpath语法:/(定位路径)、@(获取属性)
List<Node> actionNodes = docu.selectNodes("/config/action");
//循环遍历action节点
for (Node action : actionNodes) {
//将action节点转换成元素节点
Element actionElem = (Element) action;
//逐一获取action节点中的属性path\type
String actionPath = actionElem.attributeValue("path");
String actionType = actionElem.attributeValue("type");
//初始化ActionModel并完成建模赋值操作
actionModel = new ActionModel();
actionModel.setPath(actionPath);
actionModel.setType(actionType);
//获取action节点下的forward节点0~N
List<Node> forwardNodes = actionElem.selectNodes("forward");
//循环遍历所有的forward节点
for (Node forward : forwardNodes) {
//将forward节点转换成元素节点
Element forwardElem = (Element) forward;
//获取forward节点中的所有属性 name、path、redirect
String forwardName = forwardElem.attributeValue("name");
String forwardPath = forwardElem.attributeValue("path");
String forwardRedirect = forwardElem.attributeValue("redirect");
//创建forwardModel建模对象并完成赋值操作
forwardModel = new ForwardModel();
forwardModel.setName(forwardName);
forwardModel.setPath(forwardPath);
forwardModel.setRedirect(Boolean.parseBoolean(forwardRedirect));
//action节点中包含0~N个forward九二点,所以请将forward节点的建模对象添加至actionModel中
actionModel.push(forwardModel);
}
//config节点中包含0~N个action节点,所以将action点的建模对象添加至configModel中
configModel.push(actionModel);
}
} catch (Exception e) {
e.printStackTrace();
}
return configModel;
}
public static void main(String[] args) {
ConfigModel configModel = ConfigModelFactory.createConfigModel(DEFAULT_PATTH);
//包含关系:ConfigModel -- ActionModel -- ForwardModel
//要求:从config节点中获取action节点的name属性为/loginAction的节点
ActionModel actionModel = configModel.get("/loginAction");
System.out.println("path="+actionModel.getPath());
System.out.println("re="+actionModel.getType());
//要求:获取action节点中某一个forward节点
ForwardModel forwardModel = actionModel.get("success");
System.out.println("name="+forwardModel.getName());
System.out.println("path="+forwardModel.getPath());
System.out.println("redirect="+forwardModel.isRedirect());
}
}