DTD-xml建模
2019-05-27 20:32
337 查看
文章目录
1、以面向对象的编程思想,描述xml资源文件
所谓的以面向对象的编程思想,主要的就是把一个标签当做对象,他的属性值当做元素,例:
public class ForwardModel { private String name; private String pathl; private boolean redirect; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPathl() { return pathl; } public void setPathl(String pathl) { this.pathl = pathl; } public boolean isRedirect() { return redirect; } public void setRedirect(boolean redirect) { this.redirect = redirect; }
2、将xml文件中内容封装进model实体对象。
也就是上文提到的把标签当做对象,他的属性值当做元素,然后运用map集合存入他的上一级标签对象里进行操作。
当每个标签完成封装时,运用工厂模式,进行操作:
public class CongigModelFactory { public static ConfigModel build() throws Exception { return build("config.xml"); } private static ConfigModel build(String xmlpath) throws Exception { }
在方法里面写好主要代码,增强复用性
private static ConfigModel build(String xmlpath) throws Exception { ConfigModel configModel = new ConfigModel(); InputStream is = CongigModelFactory.class.getResourceAsStream(xmlpath); SAXReader saxreader = new SAXReader(); Document doc = saxreader.read(is); 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.setPathl(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 = CongigModelFactory.build(); ActionModel actionModel = configmodel.pop("/loginAction"); //System.out.println(actionModel.getType()); ForwardModel forwardModel = actionModel.pop("failed"); System.out.println(forwardModel.getPathl()+" "+forwardModel.isRedirect()); }
相关文章推荐
- 超越学XML&DTD笔记技术总结(之一)
- [xml]DTD & Schema 约束
- 设置eclipse中xml的默认编辑器、行数、xsd和dtd
- PHP DOM验证xml文档是否符合DTD验证
- xml约束文档之DTD
- Eclipse添加DTD文件实现xml的自动提示功能
- MyEclipse添加ibatis DTD文件实现xml的自动提示功能
- Eclipse里的No grammar constraints (DTD or XML schema) detected for the document警告
- XML建模
- Warning: No grammar constraints (DTD or XML schema) detected for the document
- XML与DTD
- php xml dtd 验证
- xml中dtd中ELEMENT元素的类型种类
- XML总结. <DTD定义初级>
- XMl中DTD校验案例
- xml中的DTD
- Java程序员从笨鸟到菜鸟之(二十四)Xml基础详解和DTD验证
- pet.template,NewFile.xml,pets.dtd(这是三个文件)
- XML-为XML添加DTD-Schema方法
- XML DTD详解(转)