XML建模
2019-05-27 19:34
281 查看
1、建模的由来
就是将指定的xml字符串当作对象来操作
如果说当对一个指定的xml格式字符串完成了建模操作,
好处在于,只需要调用指定的方法就可以完成预定的字符串获取;
2、建模的思路
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象
3、建模的好处 :(提高代码的复用性)
建模分两步:
1、以面向对象的编程思想,描述xml资源文件
2、将xml文件中内容封装进model实体对象。
Demo:
config.xml
<?xml version="1.0" encoding="UTF-8"?> <config> <action path="/registerAction" type="test.action.RegisterAction"> <forward name="success" path="/index.jsp" redirect="true" /> <forward name="failed" path="/register.jsp" redirect="false" /> </action> <action path="/loginAction" type="test.action.LoginAction"> <forward name="a" path="/index.jsp" redirect="false" /> <forward name="b" path="/welcome.jsp" redirect="true" /> </action> </config>
ForwardModel
package com.zking.xml.model; public class ForwardModel { // <forward name="success" path="/index.jsp" redirect="true" /> 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; } }
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); } }
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); } }
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"); } public 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")); configModel.push(actionModel); 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.getPath() + " " + forwardModel.isRedirect()); } }
注:属性为String类型,子元素标签则是map的值,子元素标签的唯一标识则为map的值