xml modeling

1. What is XML modeling
to XML configuration file elements, attributes, text information into objects called XML modeling process.
2.XML model
creating elements according to the XML configuration file element nodes, node, entity class
ConfigModel ActionModel ForwardModel
using XML technology dom4j + xpath modeling ConfigModelFactory (increase the reusability of code)

Modeling ideas

1, the analysis needs to be modeled in the file that has several objects
2, each object has behavior and attributes
3, according to the XML element node circumstances ConfigModel, ActionModel, ForwardModel object model. (Definition objects from inside to outside)

 

ConfigModelFactory.java

 
 

 

. 1 2 Package com.zking.model; . 3 Import a java.io.InputStream;
 . 4 Import java.util.List;
 . 5 . 6 Import org.dom4j.Document;
 . 7 Import org.dom4j.DocumentException;
 . 8 Import org.dom4j.Element;
 . 9 Import org.dom4j.io.SAXReader;
 10 . 11 / ** 12 is one of the design pattern 23 *
 13 * design pattern is a solution to the search process in Java encountered a problem specific
 14 * factory pattern
 15 * resolved what is the problem?
16 * It is used to produce the entity class resource file specified
 17 * benefits:
 18
* Improve the reusability of code . 19 * 20 is * @author the Admin 21 is * 22 is * / 23 is public class ConfigModelFactory { 24 25 public static ConfigModel Build () throws DocumentException { 26 is return ConfigModel ( "the config.xml" ); 27 } 28 29 / ** 30 * to produce a receiving entity class has the class configModel 31 is * @param XMLPath 32 * @return 33 is * @throws DocumentException 34 */ 35 private static ConfigModel ConfigModel(String xmlPath) throws DocumentException { 36 ConfigModel configModel=new ConfigModel(); 37 ActionModel actionModel=null; 38 ForwardModel forwardModel=null; 39 InputStream in=ConfigModelFactory.class.getResourceAsStream(xmlPath); 40 SAXReader saxReader=new SAXReader(); 41 Document doc= saxReader.read(in); 42 List<Element> actionEles =doc.selectNodes("/config/action"); 43 for (Element actionEle : actionEles) { 44 actionModel=new ActionModel(); 45 //给actionModel对象填充xml中的action标签的类容 46 actionModel.setPath(actionEle.attributeValue("path")); 47 actionModel.setPath(actionEle.attributeValue("type")); 48 List<Element> forwardEles=actionEle.selectNodes("forward"); 49 for (Element forwardEle : forwardEles) { 50 forwardModel=newForwardModel (); 51 is // class content to forwardModel action tag in the xml object fill 52 is forwardModel.setName (forwardEle.attributeValue ( "name" )); 53 is forwardModel.setPath (forwardEle.attributeValue ( "path" )); 54 is forwardModel.setRedirect (! "false" .equals (forwardEle.attributeValue ( "redirect" ))); 55 // redirect default is true redirect 56 // only filled out false is forwarded 57 // forwardEle.attributeValue ( "redirect ') is the XML you get filled value 58 // not fill redirect "false" .equals (forwardEle. attributeValue("Redirect"))是false 59 // 填 true 重定向 "false".equals(forwardEle.attributeValue("Redirect"))是false 60 // 填 false 转发 "false".equals(forwardEle.attributeValue("Redirect"))是true 61 62 63 actionModel.push(forwardModel); 64 } 65 configModel.push(actionModel); 66 } 67 return configModel; 68 } 69 70 public static void main(String[] args) throws DocumentException { 71 ConfigModel configModel=ConfigModelFactory.build(); 72 ActionModel actionModel=configModel.pop("/loginAction"); 73 System.out.println(actionModel.getType()); 74 75 } 76 77 78 }

ConfigModel.java

package com.zking.model;

import java.util.HashMap;
import java.util.Map;

public class ConfigModel {

    private Map<String, ActionModel> amap=new HashMap<>();
    
    public void push(ActionModel actionmdodel) {
        amap.put(actionmdodel.getPath(), actionmdodel);
    }
    
    public ActionModel pop(String path) {
        return amap.get(path);
    }
    
    public static void main(String[] args) {
        ConfigModel configModel=new ConfigModel();
        ActionModel actionModel=configModel.pop("/loginAction");
        System.out.println(actionModel.getType());
        
    }
}
ForwardModel.java
package com.zking.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;
    }
    
    
    
}
ActionModel.java
package com.zking.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<>();
    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);
    }
    
    
}
config.xml
 
<? XML Version = "1.0" encoding = "UTF-. 8"?> <-! 
        Action Tags: can full 0 ~ N th forward tag 
        path: in / at the beginning of the string and a non-null value must be unique 
        type: Character string, a non-empty
     -> 
    <-! 
        config tags: can contain 0 ~ N th action tag 
     -> 
<config> 
    
    <action path = "/ regAction" type = "test.RegAction"> 
        <-! 
            Forward label : no sub-labels; 
            name: string, forward tag name value under the same action tag must be different; 
            path: in / string beginning 
            redirect: only to false | to true , allowing empty, the default value to false
         -> 
        <name = Forward "failed"path="/reg.jsp" redirect="false" />
        <forward name="success" path="/login.jsp" redirect="true" />
    </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>

 

 

1. What is XML modeling
to XML configuration file elements, attributes, text information into objects called XML modeling process.
2.XML model
creating elements according to the XML configuration file element nodes, node, entity class
ConfigModel ActionModel ForwardModel
using XML technology dom4j + xpath modeling ConfigModelFactory (increase the reusability of code)

Modeling ideas

1, the analysis needs to be modeled in the file that has several objects
2, each object has behavior and attributes
3, according to the XML element node circumstances ConfigModel, ActionModel, ForwardModel object model. (Definition objects from inside to outside)

 

Guess you like

Origin www.cnblogs.com/bf6rc9qu/p/11006004.html