freemarker快速生成xml文件

Posted stormMoonlet

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了freemarker快速生成xml文件相关的知识,希望对你有一定的参考价值。

记录一次用freeMarker生成xml文件

import freemarker.template.Configuration;
import freemarker.template.Template;
/*
*uiName 要生成xml文件的名称
*xmlName 模板ftl的名称
*/

public static void createXml(String uiName,String xmlName) {
Writer w = null;
      try {
      //获取xmlTemplate文件夹的当前路径
        URL url = Thread.currentThread().getContextClassLoader().getResource("xmlTemplate");
      String path = url.getPath();
        Configuration configuration = new Configuration();
      configuration.setDefaultEncoding("utf-8");
      configuration.setDirectoryForTemplateLoading(new File(path));
      //解决写入到xml文件出现乱码问题
      Template template = configuration.getTemplate(xmlName+".ftl","utf-8");
       Map<String, Object> responseMap = new HashMap<String, Object>();
              responseMap.put("id", "1");
              responseMap.put("name", "编号");
              responseMap.put("value", "007");
      File file = new File("D:\\"+uiName+".xml");
      FileOutputStream f = new FileOutputStream(file);
       w = new OutputStreamWriter(f,"utf-8");
      template.process(responseMap, w);
      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
    w.close();
  }
  }


ftl模板

<?xml version="1.0" encoding="UTF-8"?>
<properties>
  <property>
    <id>${id!}</id>
    <name>${name!}</name>
    <value>${value!}</value>
  </property>
</properties>



以上是关于freemarker快速生成xml文件的主要内容,如果未能解决你的问题,请参考以下文章

FreeMarker 快速入门

FreeMarker 快速入门学习

Java 使用模板生成 Word 文件---基于 Freemarker 模板框架

freemarker生成word文档

使用freemarker模板引擎生成word文档的开发步骤

利用freemarker生成word,word另存为xml文件的标签解析