xml建模
Posted xmf3628
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml建模相关的知识,希望对你有一定的参考价值。
1.建模的由来
就是将指定的xml字符串当作对象来操作
如果说当对一个指定的xml格式字符串完成了建模操作,
好处在于,只需要调用指定的方法就可以完成预定的字符串获取;
2.建模的思路
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象
好处:
提高代码的复用性
分析XML文件(有几个对象以及属性,定义对象从小到大(从里到外))
<?xml version="1.0" encoding="UTF-8"?>
<!-- config标签:可以包含0~N个action标签 -->
<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>
定义XML模型对象
1.ForwardModel
public class ForwardModel
// <forward name="failed" path="/login.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;
2.ActionModel
public class ActionModel
// <action path="/loginAction" type="test.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 Map<String, ForwardModel> getFmap()
return fmap;
public void setFmap(Map<String, ForwardModel> fmap)
this.fmap = fmap;
//添加
public void push(ForwardModel forwardModel)
fmap.put(forwardModel.getName(), forwardModel);
//通过name获取对象
public ForwardModel pop(String name)
return fmap.get(name);
ConfigModel
private Map<String, ActionModel> amap=new HashMap();
public void push(ActionModel actionModel)
amap.put(actionModel.getPath(), actionModel);
public ActionModel pop(String path)
return amap.get(path);
ConfigModelFactory(工厂模式用来将资源文件生产指定的实体类)
public class ConfigModelFactory
public static ConfigModel build() throws Exception
return configModel("config.xml");
/**
* 生产出有内容的实体类configModel
* @param xmlPath
* @return
* @throws Exception
*/
public static ConfigModel configModel(String xmlPath) throws Exception
ConfigModel configModel=new ConfigModel();
ActionModel actionModel=null;
ForwardModel forwardModel=null;
InputStream in=ConfigModelFactory.class.getResourceAsStream(xmlPath);
SAXReader sax=new SAXReader();
Document doc=sax.read(in);
List<Element> actionEles= doc.selectNodes("/config/action");
for (Element actionEle : actionEles)
actionModel=new ActionModel();
//给actionModel对象填充xml中的action标签的内容
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
List<Element> forwardEles=actionEle.selectNodes("forward");
for (Element forwardEle : forwardEles)
forwardModel=new ForwardModel();
//给forwardModel对象填充xml中的forward标签的内容
forwardModel.setName(forwardEle.attributeValue("name"));
forwardModel.setPath(forwardEle.attributeValue("path"));
forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue(("redirect"))));
// <forward name="failed" path="/reg.jsp" redirect="false" />
// redirect默认是true 重定向 只有填了false才是转发
//forwardEle.attributeValue(("redirect"))拿到的是xml中你所填的值
// 不填 重定向 "false".equals(forwardEle.attributeValue(("redirect")))是false
// 填 true 重定向 "false".equals(forwardEle.attributeValue(("redirect")))是false
// 填flase 转发 "false".equals(forwardEle.attributeValue(("redirect")))true;
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");
ForwardModel forwardModel=actionModel.pop("success");
System.out.println( actionModel.getType());
System.out.println(forwardModel.getPath());
对web.xml进行建模,链接 https://i.cnblogs.com/Files.aspx
以上是关于xml建模的主要内容,如果未能解决你的问题,请参考以下文章
xml Eclipse模板(代码片段)检查参数并最终抛出IllegalArgumentException
从 XML 声明片段获取 XML 编码:部分内容解析不支持 XmlDeclaration
[新增ST-001片段]全程字幕-20套UML+Enterprise Architect建模示范视频