当前位置:网站首页>XML modeling

XML modeling

2022-06-25 02:59:00 Chasing dream Zichen

utilize dom4j+xpath Technical realization XML modeling

Mind mapping :

 Insert picture description here
Case code :



public class ConfigTe {
    
    // Configure default address 
    private static final String defual_path="../J2EE05/config.xml";

    private ConfigTe() {
    
    }

    /** *  Modeling methods  * @param path  Address  * @return * @throws DocumentException */

    public static ConfigMod createConfigTe(String path) throws DocumentException {
    
        String actiontype = null;
        String actionpath = null;
        String name = null;
        String paths = null;
        String redirect= null;
        ActionMod am = null;
        // obtain io flow 
        InputStream is = ConfigTe.class.getResourceAsStream(path);
        // Reader 
        SAXReader sr = new SAXReader();
        // Read configuration file , obtain document object 
        Document doc = sr.read(is);
        // Use xpath Parse object 
        //config node 
        List<Node> config = doc.selectNodes("config");

        ConfigMod configMod = new ConfigMod();
        for (Node c : config
        ) {
    

            //action node 
            List<Node> action = c.selectNodes("action");
            for (Node a : action
            ) {
    
                am = new ActionMod();

                Element ea = (Element) a;
                actiontype = ea.attributeValue("type");
                actionpath = ea.attributeValue("path");
                am.setPath(actionpath);
                am.setType(actiontype);

                //forward node 
                List<Node> forward = a.selectNodes("forward");
                for (Node f : forward
                ) {
    
                    Element el = (Element) f;
                     name = el.attributeValue("name");
                     paths = el.attributeValue("path");
                     redirect = el.attributeValue("redirect");
                  ForwardMod  fm = new ForwardMod(name, paths, redirect);

                    am.put(fm);
                }
                configMod.put(am);
            }



        }
        return configMod;
    }
    public static ConfigMod createConfigTe() throws DocumentException {
    
        return createConfigTe(defual_path);
    }



    public static void main(String[] args) throws DocumentException {
    
        ConfigMod configTe = ConfigTe.createConfigTe();
        ActionMod s = configTe.get("/regAction");
        System.out.println(s);

        ForwardMod f  =s.get("failed");
        System.out.println(f);

    }






}

With ActionMod For example :



public class ActionMod {
    
    private Map<String,ForwardMod> action = new HashMap<>();
    private String type;
    private String path;

    @Override
    public String toString() {
    
        return "ActionMod{" +
                "action=" + action +
                ", type='" + type + '\'' +
                ", path='" + path + '\'' +
                '}';
    }




    public ActionMod(Map<String, ForwardMod> action, String type, String path) {
    
        this.action = action;
        this.type = type;
        this.path = path;
    }
    public ActionMod(Map<String, ForwardMod> action, String type) {
    
        this.action = action;
        this.type = type;
    }
    public ActionMod() {
    
    }

    public Map<String, ForwardMod> getAction() {
    
        return action;
    }

    public void setAction(Map<String, ForwardMod> action) {
    
        this.action = action;
    }

    public String getType() {
    
        return type;
    }

    public void setType(String type) {
    
        this.type = type;
    }

    public String getPath() {
    
        return path;
    }

    public void setPath(String path) {
    
        this.path = path;
    }

    /** *  save forwar object  * @param forward  object  */
    public void put(ForwardMod forward){
    
        this.action.put(forward.getName(),forward);
    }

    /** *  Get the node  * @param forname  Primary key  * @return forward object  */
    public ForwardMod get(String forname){
    
        return this.action.get(forname);
    }


}

原网站

版权声明
本文为[Chasing dream Zichen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242351585036.html