freemarker 的xml模板


public String xmlFormwork(String xmlPath, String xmlName, String format, Map<String, Object> dataMap) {
        String xmlString = "";
        // 得FreeMarker配置對象
        // 創建Configuration對象
        Configuration cfg = new Configuration(new Version("2.3.28"));
        // 設置模板編碼格式
        cfg.setEncoding(Locale.getDefault(), format);
        // 得FreeMarker的關鍵對象---------模板
        // 創建Template對象
        Template template = null;
        try {
            // 設置FreeMarker的模版文件位置
            cfg.setDirectoryForTemplateLoading(new File(xmlPath));
            cfg.setDefaultEncoding("UTF-8");
            template = cfg.getTemplate(xmlName);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        // String path = ServletActionContext.getServletContext().getRealPath("/");
        /*
         * File dir = new File(path + htmlPath); if (!dir.exists()) { dir.mkdirs(); }
         */
        // File fileName = new java.io.File(path + htmlPath + htmlName);
        // System.out.println("html file:" + fileName.getPath());
        // Writer writer = null;
        StringWriter w = new StringWriter();
        try {
            System.out.println(w.toString());
            // 生成xml
            template.process(dataMap, w);
            w.close();
            //
            System.out.println("w.toString()");
            System.out.println(w.toString());
            System.out.println("toString()");
            xmlString = w.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return xmlString;
    }

xml模板參考

 https://blog.csdn.net/shuzhupeng/article/details/79981736


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM