您的位置:首页 > 其它

XML建模

2019-05-27 22:07 344 查看

建模的由來

将指定的xml字符串当作对象来操作,如果说当对一个指定的xml格式字符串完成了建模操作,好处在于,只需要调用指定的方法就可以完成预定的字符串获取;

XML建模思路

开始的实际分析XML文件中有几个对象(标签)
理清楚每个对象(标签) 有的行为和能有的操作
在定义对象时从小到大(从里到外)
通过23种的设计模式中的工厂模式,解析xml生产出指定对象
下面是我寫的一個簡單的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--
action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 ,子控制器对应的路径
type:字符串,非空,子控制器的完整类名
-->
<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>

你知道有多少标签吗?
有三个:config ,action ,forward
就是创建三个基础类:

forwardModel类
public class ForwardModel {

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类
public class ActionModel {

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类
public class ConfigModel {

private Map<String, ActionModel> acmap=new HashMap<>();
//因为name是唯一,所以可以把name当成建,通过map集合来取值
public void push(ActionModel actionmodel) {
acmap.put(actionmodel.getPath(), actionmodel);
}

public ActionModel pop(String path) {
return acmap.get(path);
}

}
ConfigModelFactory类
public class ConfigModelFatory {

public static ConfigModel build() throws Exception {
return build("config.xml");
}

public static ConfigModel build(String string) throws Exception {
ConfigModel configmodel=new ConfigModel();
InputStream in = ConfigModelFatory.class.getResourceAsStream("config.xml");
SAXReader reader=new SAXReader();
Document doc = reader.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"));
//拿到foward
List<Element> forword = actionEle.selectNodes("forward");
for (Element forwardEle : forword) {
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=ConfigModelFatory.build();
ActionModel actionmodel=configmodel.pop("/loginAction");
System.out.println(actionmodel.getType());
ForwardModel forwardmodel = actionmodel.pop("a");
System.out.println(forwardmodel.getPath()+"     "+forwardmodel.isRedirect());
}

}

取到属性相应的值

今天就更新到这里
喜欢的可以关注我
不定时更新

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: