Introduction to modeling and ideas
The origin of modeling : xml string is specified as objects to manipulate
Modeling advantages : If one when specified xml format string modeling operation is completed, only need to call the specified method to complete a predetermined character string acquired.
Modeling ideas :
1, the analysis needs to be modeled in a file that has several objects
2, each object has behavior and attributes
3, the definition of small to large objects (inside and out)
4, by 23 kinds of design pattern model of the factory, to produce the specified object parsed xml
Benefits : improve code reusability
XML Modeling
ConfigModel class
package com.zking.xml.mode;
import java.util.HashMap;
import java.util.Map;
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 static void main(String[] args) {
ConfigModel configModel = new ConfigModel(); //null a null
ActionModel a = configModel.pop("/LoginAction");
}
}
ActionModel class
package com.zking.xml.mode;
import java.util.HashMap;
import java.util.Map;
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 pash(ForwardModel forwarModel) {
fMap.put(forwarModel.getName(), forwarModel);
}
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
ForwardModel class
package com.zking.xml.mode;
public class ForwardModel {
// <forward name="a" path="/index.jsp" redirect="false" />
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;
}
}
ConfigModelFactory类
package com.zking.xml.mode;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigModelFatory {
public static ConfigModel build() throws Exception {
return build("config.xml");
}
private static ConfigModel build(String xmlPath) throws Exception {
ConfigModel configModel = new ConfigModel();
InputStream in = ConfigModelFatory.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中填充内容
forwardModel.setName(forwardEle.attributeValue("name"));
forwardModel.setPath(forwardEle.attributeValue("path"));
forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
actionModel.pash(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
public static void main(String[] args) throws Exception {
ConfigModel configModel = ConfigModelFatory.build();
ActionModel actionModel = configModel.pop("/LoginAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel = actionModel.pop("b");
System.out.println(forwardModel.getName() + " " + forwardModel.isRedirect());
}
}
After output
NOTE : attribute is of type String tag is the value sub-element map uniquely identifies the sub-element tag values was the map
modeled in two steps :
1, object-oriented programming ideas, described xml resource file
2, the xml file into the contents of the package entity object model.
To learn more, private letter bloggers.