zoukankan      html  css  js  c++  java
  • freemarker 用template快速构造XML

    freemarker 用template快速构造XML
     
    1. 需要jar freemarker-2.3.8.jar
    2. demo 如下:
    import java.io.File;
    import java.io.IOException;
    import java.io.StringWriter;
    import java.util.HashMap;
    import java.util.Map;
    
    import freemarker.core.Environment;
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateExceptionHandler;
    
    
    public class Test {
    	/**
         * <一句话功能简述>
         * <功能详细描述>
         * @param map map
         * @param fileName fileName
         * @return 字符串流
         * @see [类、类#方法、类#成员]
         */
        @SuppressWarnings("unchecked")
        public static String buil(Map map, String fileName)
        {
            String url = Test.class.getResource("").getPath().replaceAll("%20", " ");
            String path = url;
            StringWriter out = new StringWriter();
            try
            {
                Configuration configuration = new Configuration();
                configuration.setDirectoryForTemplateLoading(new File(path));
    
                Template template = configuration.getTemplate(fileName);
                template.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
                template.setEncoding("UTF-8");
                template.setOutputEncoding("UTF-8");
                Environment env = template.createProcessingEnvironment(map, out);
    
                env.process();
                out.flush();
            }
            catch (Exception e)
            {
    
            }
            finally
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
    
            return out.toString();
        }
    public static void main(String[]arrgs)
    {
    	Map<String,String> map= new HashMap<String,String>();
    	map.put("id", "test123456");
    	System.out.println(buil(map,"test.ftl"));
    }
    }
    

    test.ftl
    <xml id ="${id}">
    </xml>
    参考网站如下:
  • 相关阅读:
    AngularJS笔记---数据绑定
    Javascript笔记--函数
    C#笔记---动态类(Dynamic)应用
    Javascript笔记--Objects
    Javascript笔记----实现Page页面右下角置顶按钮.
    C#基础---扩展方法的应用
    .Net程序员之Python基础教程学习----函数和异常处理[Fifth Day]
    1.3 函数式接口
    1.2 lambda 表达式的语法
    1.1 为什么要使用lambda 表达式
  • 原文地址:https://www.cnblogs.com/xue88ming/p/7182998.html
Copyright © 2011-2022 走看看