XML建模
建模的由来
1.就是将指定的xml字符串当作对象来操作
如果说当对一个指定的xml格式字符串完成了建模操作,
好处在于,只需要调用指定的方法就可以完成预定的字符串获取;
建模的思路
1、分析需要被建模的文件中有那几个对象
比如我们需要对config.xml进行建模我们就需要建立几个包
2、每个对象拥有的行为以及属性
其次我们要对需要分析的xml属性进行封装如下代码
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;
}
}
每个对象的属性都要进行封装:
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);
}
}
3、定义对象从小到大(从里到外)
再需要进行分析的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);
}
}
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象
为什么需要工厂模式:
是为了提高代码的复用性
如下代码:
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigModelFoctory {
public static ConfigModel bulid() throws DocumentException {
return bulid("config.xml");
}
private static ConfigModel bulid(String xmlPath) throws DocumentException {
// TODO Auto-generated method stub
ConfigModel configModel = new ConfigModel();
InputStream in = ConfigModelFoctory.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.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 DocumentException {
ConfigModel configModel =ConfigModelFoctory.bulid();
ActionModel actionModel = configModel.pop("/loginAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel =actionModel.pop("a");
System.out.println(forwardModel.getPath()+""+ forwardModel.isRedirect());
}
}
得到最后的结果
最后总结;建模分两部
1、以面向对象的编程思想,描述xml资源文件
2、将xml文件中内容封装进model实体对象。