1.XML的由来
1.就是将指定的xml字符串当作对象来操作.
2.如果说当对一个指定的xml格式字符串完成了建模操作.
3.好处:只需要调用指定的方法就可以完成预定的字符串获取.
2.建模的思路
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象
5.好处:提高代码的复用性
3.应用
在XML建模中用几个类来获取哦 config.xml中的属性
注:属性为String类型,子元素标签则是map的值,子元素标签的唯一标识则为map的值
<?xml version="1.0" encoding="UTF-8"?>
<config>
<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>
我们来分析一下XML中哪几个对象
public class ConfigModel {
private Map<String , ActionModel> acMap = new HashMap();
public void push(ActionModel actionModel) {
acMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return acMap.get(path);
}
}
public class ActionModel {
// <action path="/loginAction" type="test.action.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 void push(ForwardModel forwardModel) {
fMap.put(forwardModel.getName(),forwardModel);
}
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
public class ConfigModel {
private Map<String , ActionModel> acMap = new HashMap();
public void push(ActionModel actionModel) {
acMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return acMap.get(path);
}
最后通过工厂模式,解析XML生产的指定对象
public class ConfigModelFactory {
public static ConfigModel build() throws Exception {
return build("config.xml");
}
private 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.setType(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
List<Element> forwardEles = actionEle.selectNodes("forward");
for (Element forwardEle : forwardEles) {
forwardModel = new ForwardModel();
//接下来需要往actionModel中填充内容
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 configModer = ConfigModelFactory.build();
ActionModel actionModel = configModer.pop("/loginAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel = actionModel.pop("b");
System.out.println(forwardModel.getPath() + " " + forwardModel.isRedirect());
}
运行结果:
这样我们就取到属性相应的值
建模分成两步
1、以面向对象的编程思想,描述xml资源文件
2、将xml文件中内容封装进model实体对象。
制作不易,点个赞再走呗!!!