XML建模

简介: XML建模

1.封装对象

1.以面向对象的思想形容/封装对象,形容的是xml文件

列如:

<?xml version="1.0" encoding="UTF-8"?>
  <!--
    config标签:可以包含0~N个action标签
  -->
<config>
  <action path="/regAction" type="test.RegAction">
    <forward name="failed" path="/reg.jsp" redirect="false" />
    <forward name="success" path="/login.jsp" redirect="true" />
  </action>
<action path="/regAction" type="test.RegAction">
    <forward name="add" path="/bookadd.jsp" redirect="false" />
         <forward name="del" path="/reg.jsp" redirect="false" />
          <forward name="list" path="/list.jsp" redirect="false" />
           <forward name="upd" path="/login.jsp" redirect="false" />
  </action>
  <action path="/loginAction" type="test.LoginAction">
    <forward name="failed" path="/login.jsp" redirect="false" />
    <forward name="success" path="/main.jsp" redirect="true" />
  </action>
</config>

这是一个xml文件

package model;
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;
}
@Override
public String toString() {
  return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}

这是封装了他的forwar标签

package model;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
private String path;
private String type;
private Map<String,ForwardModel> fmap=new HashMap<String,ForwardModel>();
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往ation放压载
public void push(ForwardModel forwardModel) {
  fmap.put(forwardModel.getName(),forwardModel);
}
//从action中取值
public ForwardModel pop(String name) {
  return fmap.get(name);
}
}

这是封装了action标签

package model;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.DocumentException;
public class 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) throws DocumentException {
  ConfigModel configModel =new ConfigModelFactory().build();
  ActionModel actionModel = configModel.pop("/loginAction");
  ForwardModel forwardModel=actionModel.pop("success");
  System.out.println(forwardModel.getPath());
}
}

这是封装了他的config标签

以上便是xml的封装实体类

2.xml建模

2.1.将xml的内容初始化到 描述出的模型对象中的过程称之为建模

2.2用到了23中设计模式中的工厂模式

如:

package model;
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;
import com.sun.xml.internal.fastinfoset.tools.StAX2SAXReader;
public class ConfigModelFactory {
public static ConfigModel build() throws DocumentException {
  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());
  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"));
  List<Element> forwardEles=actionEle.selectNodes("forward");
  for (Element element : forwardEles) {
   ForwardModel forwardModel = new ForwardModel();
   forwardModel.setName(element.attributeValue("name"));
   forwardModel.setPath(element.attributeValue("path"));
   forwardModel.setRedirect(!"false".equals(element.attribute("false")));
  System.out.println(element.getPath());
   actionModel.push(forwardModel);
  }
  configModel.push(actionModel);
}
  return configModel;
}
public static void main(String[] args) throws DocumentException {
  ConfigModelFactory.build();
}
}

以上便是xml建模的全过程

当我们运行这段代码时:

public static void main(String[] args) throws DocumentException {
  ConfigModel configModel =new ConfigModelFactory().build();
  ActionModel actionModel = configModel.pop("/loginAction");
  ForwardModel forwardModel=actionModel.pop("success");
  System.out.println(forwardModel.getPath());
}

结果为:

以上便是建模全内容


相关文章
|
XML Java 数据格式
一.Xml建模之入门
一.Xml建模之入门
71 1
|
XML 存储 JSON
XML建模最详细介绍讲解
XML建模最详细介绍讲解
104 0
|
XML 存储 设计模式
XML系列(三)之 XML建模
XML系列(三)之 XML建模
|
XML 设计模式 开发框架
J2EE(xml建模)
J2EE(xml建模)
61 0
|
XML 数据格式
J2EE-XML建模(最详细之篇)
J2EE-XML建模(最详细之篇)
35 0
|
XML 设计模式 开发框架
J2EE之XML建模(超详细)
J2EE之XML建模(超详细)
56 0
|
XML 存储 开发框架
J2EE之XML建模
J2EE之XML建模
43 0
|
XML 存储 设计模式
xml建模
xml建模
57 0
|
XML 存储 设计模式
XML建模
XML建模
49 0
|
XML 存储 Java
XML之建模
XML之建模
77 0