XML建模
一:XML建模
**
建模的核心就是利用java面向对象的特性,用操作对象的方式来操作XML**
二:XML建模思路步骤
1、分析需要被建模的文件中有几个对象(标签);
2、分析对象拥有的行为以及属性;
3、注意:定义对象从小到大 ,从里到外;
4、通过设计模式中的工厂模式,解析xml生产出指定对象。
三:XML实例演示
3.1 解析资源文件
<config>
<!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 type:字符串,非空 -->
<action path="/regAction" type="test.RegAction">
<!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串
redirect:只能是false|true,允许空,默认值为false -->
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action>
<action path="/loginAction" type="test.LoginAction">
<forward name="failed" path="/login.jsp" redirect="false" />
<forward name="success" path="/main.jsp" redirect="true" />
</action>
</config>
3.2 需求
3.3 分析
3.4 ForwardModel类
public class ForwardModel {
// <forward name="failed" path="/reg.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;
}
}
3.5 ActionModel类
将指定的forwardModel压入当前actionModel对象中
public class ActionModel {
//<action path="/regAction" type="test.RegAction">
private String path;
private String type;
private Map<String, ForwardModel> forMap=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) {
forMap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return forMap.get(name);
}
}
3.6 ConfigModel类
**将指定的actionModel压入当前configModel对象中**
public class ConfigModel {
private Map<String, ActionModel> acMap=new HashMap<String, ActionModel>();
public void push(ActionModel actionModel) {
acMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return acMap.get(path);
}
}
3.7 ConfigModelFactory类
**工厂模式解决问题 将代码封装 提高代码的复用性
一般工厂类一定会有一个方法 就是生产指定模型对象的方法**
public class ConfigModelFactory {
// 动态读取任意位置下的构架配置文件
public static ConfigModel build(String path) throws Exception {
InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
SAXReader reader=new SAXReader();
// doc--->/config.xml的内容
Document doc = reader.read(in);
// 把内容填充到configModel对象中
ConfigModel configModel=new ConfigModel();
ActionModel actionModel=null;
ForwardModel forwardModel=null;
List<Element> actionLs = doc.selectNodes("/config/action");
for (Element actionL : actionLs) {
actionModel=new ActionModel();
actionModel.setPath(actionL.attributeValue("path"));
actionModel.setType(actionL.attributeValue("type"));
// 拿到forward标签内容
List<Element> forwardLs = actionL.selectNodes("forward");
for (Element forwardL : forwardLs) {
forwardModel =new ForwardModel();
forwardModel.setName(forwardL.attributeValue("name"));
forwardModel.setPath(forwardL.attributeValue("path"));
forwardModel.setRedirect(!"false".equals(forwardL.attribute("redirect")));
}
configModel.push(actionModel);
}
return configModel;
}
// 默认框架路径的模型对象构建方法
public static ConfigModel build() throws Exception {
return build("config.xml");
}
}
3.8 运行结果
四:总结
请大家多多指教!!