以面向对象的思想,将XML配置文件中的元素 属性 文本信息 转换成对象的过程叫做XML建模.
好处:有了建模后就可以用简单的代码来调用指定字符串。
二:XML建模思路- 开始的实际分析XML文件中有几个对象(标签)
- 理清楚每个对象(标签) 有的行为和能有的操作
- 在定义对象时从小到大(从里到外)
- 通过23种的设计模式中的工厂模式,解析xml生产出指定对象
ps: 利用dom4j+xpath技术实现
四:案例演示----------------------------------------对config.xml文件的建模---------------------------------------------------
----------------------------------------------------ForwardModel---------------------------------------------------------
package com.zking.test; //public class ForwardModel { private String name; private String path; private boolean redirect; public ForwardModel() { // TODO Auto-generated constructor stub } public ForwardModel(String name, String path, boolean redirect) { super(); this.name = name; this.path = path; this.redirect = 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) { if("true".equals(redirect)) { this.redirect =true ; } } @Override public String toString() { return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]"; } }
-------------------------------------------------------- ActionModel-------------------------------------------------------
package com.zking.test; import java.util.HashMap; import java.util.Map; /// //// // public class ActionModel { private String path; private String type; // 根据路径查询信息 private Map ms; // 添加 public void push(ForwardModel fModel) { if (ms == null) { ms = new HashMap (); } ms.put(fModel.getName(), fModel); } // 取出 public ForwardModel pop(String name) { return ms.get(name); } public ActionModel() { // TODO Auto-generated constructor stub } public ActionModel(String path, String type) { super(); this.path = path; this.type = type; } 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; } @Override public String toString() { return "ActionModel [path=" + path + ", type=" + type + ", ms=" + ms + "]"; } }
----------------------------------------------------ConfigModel------------------------------------------------------------
package com.zking.test;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map ms;
// 添加
public void push(ActionModel aModel) {
if (ms == null) {
ms = new HashMap();
}
ms.put(aModel.getPath(), aModel);
}
// 取出
public ActionModel pop(String path) {
return ms.get(path);
}
public ConfigModel() {
// TODO Auto-generated constructor stub
}
}
----------------------------------------------------------ConfigModelFactory--------------------------------------------
package com.zking.test;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigModelFactory {
public static ConfigModel build(String location) throws Exception {
//获取资源
InputStream stream = ConfigModelFactory.class.getResourceAsStream(location);
if(stream==null) {
System.out.println("xml路径有问题"+location);
}
//解析器
SAXReader saxReader = new SAXReader();
//文档对象
Document document = saxReader.read(stream);
//ConfigModel模型
ConfigModel cModel=new ConfigModel();
//解析根标签
Element e = (Element) document.selectSingleNode("/config");
if(e==null) {
throw new RuntimeException("xml根节点必须为config");
}
//解析action
List e2 = document.selectNodes("action");
for (Element element : e2) {
//ActionModel模型
ActionModel aModel=new ActionModel();
//设置ActionModel模型的属性
aModel.setPath(element.attributeValue("path"));
aModel.setType(element.attributeValue("type"));
//查询forward节点
List selectNodes = element.selectNodes("forward");
for (Element element2 : selectNodes) {
//ForwardModel模型
ForwardModel fModel=new ForwardModel();
//设置ForwardModel模型的属性
fModel.setName(element2.attributeValue("name"));
fModel.setPath(element2.attributeValue("path"));
fModel.setName(element2.attributeValue("redirect"));
//将fModel添加进aModel里面
aModel.push(fModel);
}
//将aModel添加进cModel里面
cModel.push(aModel);
}
return cModel;
}
}
----------------------------------------------------test测试------------------------------------------------------------
package com.zking.test;
public class ConTest {
public static void main(String[] args) throws Exception {
// 建模
ConfigModel configModel = ConfigModelFactory.build("/com/zking/xml/config.xml");
System.out.println(configModel);
//path="/loginAction"
ActionModel model = configModel.pop("/loginAction");
System.out.println(model);
}
}



