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()); } }
取到属性相应的值
今天就更新到这里
喜欢的可以关注我
不定时更新
相关文章推荐
- 本体建模与语义Web知识发现 4 基于频繁模式挖掘的XML网页分类技术
- Web 服务建模,第 1 部分: XML 模式
- XML建模
- 【XML建模】
- Web服务建模(一):XML模式
- 本体建模与语义Web知识发现 2 XML与数据库
- Android实现XML解析技术
- applicationContext.xml详解 spring+mybatis+struts
- Element uses-permission duplicated with element declared at AndroidManifest.xml 错误
- Maven工程修改pom.xml里的packaging标签值为pom后出错,没任何提示的解决方案
- web.xml文件头出错
- 初学Android中用id来进行xml中的控件获取
- Xml文件在unity中的读写-----模拟玩游戏时我们保存游戏的动作
- 因子分析建模步骤
- 程序员笔记|3个问题带你入门数据建模
- JAVA解析XML的四种方法比较
- OPENCV(3) —— 对XML和YAML文件实现I/O 操作
- mybatis无接口直接调用xml的helloworld
- XML数据解析
- NET中Flash+Xml焦点图片轮换