XML建模
建模的由来:就是将指定的xml字符串当作对象来操作;
【优点】:当对一个指定的xml格式字符串完成了建模操作,只需要调用指定的方法就可以完成预定的字符串获取;
【好处】:提高代码的复用性;
建模的思路
①、分析需要被建模的文件中有那几个对象;
②、每个对象拥有的行为以及属性;
③、定义对象从小到大(从里到外);
④、通过23种的设计模式中的工厂模式,解析xml生产出指定对象;
建模分两步:
①、以面向对象的编程思想,描述xml资源文件
②、将xml文件中内容封装进model实体对象。
【实列】
1、XML建模 ConfigModel
package com.zking.xml.model;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String , ActionModel> acMap = new HashMap<>();
public void push(ActionModel actionModel) {
acMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return acMap.get(path);
}
}
2、XML建模 ActionModel
package com.zking.xml.model;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
// <action path="/loginAction" type="test.action.LoginAction">
private String path;
private String type;
private Map<String, ForwardModel> fMap = 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 void push(ForwardModel forwardModel) {
fMap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
3、XML建模 ForwardModel
package com.zking.xml.model;
public class ForwardModel {
// <forward name="a" path="/index.jsp" redirect="false" />
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 isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
}
4、 XML建模 ConfigModelFactory
package com.zking.xml.model;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigModelFactory {
public static ConfigModel build() throws Exception {
return build("config.xml");
}
private static ConfigModel build(String xmlPath) throws Exception {
ConfigModel configModel = new ConfigModel();
InputStream in = ConfigModelFactory.class.getResourceAsStream(xmlPath);
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(in);
ActionModel actionModel = null;
ForwardModel forwardModel = null;
List<Element> actionEles = doc.selectNodes("/config/action");
for(Element actionEle : actionEles) {
actionModel = new ActionModel();
//接下来需要往actionModel中填充内容
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
List<Element> forwardEles = actionEle.selectNodes("forward");
for (Element forwardEle : forwardEles) {
forwardModel = new ForwardModel();
//接下来需要往forwardModel中填充内容
forwardModel.setName(forwardEle.attributeValue("name")) forwardModel.setPath(forwardEle.attributeValue("path")); forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
public static void main(String[] args) throws Exception {
ConfigModel configModel = ConfigModelFactory.build();
ActionModel actionModel = configModel.pop("/loginAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel = actionModel.pop("b");
System.out.println(forwardModel.getName() + " " + forwardModel.isRedirect());
}
}
输出的结果:
注意:属性为String类型,子元素标签则是map的值,子元素标签的唯一标识则为map的值