目录
1.XML建模是什么
XML 建模是一种将数据和信息组织为一组 XML 文档或模型的方式。在 XML 建模中,可以使用标记语言的特点来表示数据和信息之间的关系,并定义其结构和格式。XML 建模可以提高数据和信息的可读性,增加其可维护性和扩展性,有助于数据和信息的交换和共享。
2. XML建模的步骤
2.2建模分两步:
2.2.3以面向对象的编程思想,描述xml资源文件
2.2.4将xml文件中内容封装进model实体对象
ConfigModel、ActionModel、ForwardModel
A.config节点下有多个子action节点,无节点属性
B.action节点下有多个子forward节点,有节点属性
C.forward下无子节点,有节点属性
首先让我们看看mvc.xml里面的内容:
如下所示:
首先我们要对它的各个节点进行对象化:
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;
}
ActionModel:
private String path;
private String type;
private Map<String, ForwardMdoel> fMap = new HashMap<String, ForwardMdoel>();
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;
}
//将forward往action中放压栈
public void push(ForwardMdoel forwardMdoel) {
fMap.put(forwardMdoel.getName(), forwardMdoel);
}
//从action中通过name值取出forward
public ForwardMdoel pop(String namr) {
return fMap.get(namr);
}
ConfigModel:
private Map<String, ActionModel> aMap = new HashMap<String, ActionModel>();
public void push(ActionModel actionModel) {
aMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return aMap.get(path);
}
public static void main(String[] args) {
ConfigModel configModel = new ConfigModel();
ActionModel actionModel = configModel.pop("/loginAction");
ForwardMdoel forwardMdoel = actionModel.pop("success");
System.out.println(forwardMdoel.getPath());
}
ConfigModelFactory:
public static ConfigModel build(String xmlPath) throws Exception {
ConfigModel configModel = new ConfigModel();
InputStream in = ConfigModelFactory.class.getResourceAsStream("mvc.xml");
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
// System.out.println(doc.asXML());
// configModel要有类容,就意味这actionModel要有类容,然后放入到configModel中去
List<Element> actionEles = doc.selectNodes("/config/action");
for (Element actionEle : actionEles) {
System.out.println(actionEle.asXML());
ActionModel actionModel = new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
// actionModel要有forwardModel,就意味这forwardModel要有类容,然后放入到actionModel中去
List<Element> forwordEles = actionEle.selectNodes("forward");
for (Element forwordEle : forwordEles) {
System.out.println(forwordEle.asXML());
ForwardMdoel forwardMdoel = new ForwardMdoel();
forwardMdoel.setName(forwordEle.attributeValue("name"));
forwardMdoel.setPath(forwordEle.attributeValue("path"));
//只有填写false才是转发,其它都为重定向
// forwardMdoel.setRedirect(false);
forwardMdoel.setRedirect(!"false".equals(forwordEle.attribute("redirect")));
actionModel.push(forwardMdoel);
}
configModel.push(actionModel);
}
return configModel;
}
public static ConfigModel build() throws Exception {
return build("/mvc.xml");
}
public static void main(String[] args) throws Exception {
ConfigModelFactory.build();
}