1、一个标签对应一个标签模型类
2、属性为String类型,子元素标签则是map类型,子元素标签的唯一标识则为map的键,子元素对应的模型类是map的值
3、通过一个根标签工厂类实例化一个根标签类(在创建根标签的时候,就是建模的过程,说白了就是将xml封装成对象)
核心代码
public class ConfigModelFactory {
public static ConfigModel build() throws Exception {
return build("config.xml");
}
public static ConfigModel build(String xmlPath) throws Exception {
ConfigModel configModel = new ConfigModel();
InputStream in = ConfigModelFactory.class.getResourceAsStream(xmlPath);
SAXReader saxReader = new SAXReader();
Document doc = saxReader.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"));
List<Element> forwardEles = actionEle.selectNodes("forward");
for (Element forwardEle : forwardEles) {
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 = ConfigModelFactory.build();
ActionModel actionModel = configModel.pop("/loginAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel = actionModel.pop("a");
System.out.println(forwardModel.getPath() + " " + forwardModel.isRedirect());
}
备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有