XML建模
建模的由来
就是将指定的xml字符串当作对象来操作
如果说当对一个指定的xml格式字符串完成了建模操作,
好处在于,只需要调用指定的方法就可以完成预定的字符串获取;
建模的思路
①、分析需要被建模的文件中有那几个对象;
②、每个对象拥有的行为以及属性;
③、定义对象从小到大(从里到外);
④、通过23种的设计模式中的工厂模式,解析xml生产出指定对象;
XML建模
我们用以下四个类来试验来获取 config.xml中的 属性
1.ConfigModel
2.ActionModel
3.ForwardModel
4.ConfigModelFactory
1、XML建模 ConfigModel
<action path="/loginAction" type="test.LoginAction">
<forward name="b" path="/login.jsp" redirect="false" />
<forward name="success" path="/main.jsp" redirect="true" />
</action>
2、XML建模 ActionModel
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
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
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());
}
}