package com.mooc.freemarkerXML;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template;
import freemarker.template.TemplateException; public class XMLTest { public static Template getTemplate(String name){
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(XMLTest.class, "/resources/");
Template template = null;
try {
template = cfg.getTemplate(name);
} catch (IOException e) {
e.printStackTrace();
}
return template;
} public static String process(String templatefile, Map<String, Object> param) throws IOException, TemplateException,
Exception{
Template template = XMLTest.getTemplate(templatefile);
StringWriter sw = new StringWriter();
template.process(param, sw);
return sw.toString();
}
public static void main(String[] args) {
Map<String, Object> responseMap = new HashMap<String, Object>();
responseMap.put("id", "1");
responseMap.put("name", "红楼梦");
responseMap.put("author", "曹雪芹");
responseMap.put("year", "1862");
responseMap.put("price", "98");
String resp = null;
try {
resp = XMLTest.process("book.ftl", responseMap);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(resp);
} }

book.ftl

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="${id}">
<name>${name}</name>
<author>${author}</author>
<year>${year}</year>
<price>${price}</price>
<address><#if address??>${address}</#if></address>
</book>
</bookstore>

XMLSerializer read()

利用freemarker生成xml的更多相关文章

  1. JSP利用freemarker生成基于word模板的word文档

    利用freemarker生成基于word模板的word文档 freemarker简介 FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器 ...

  2. 使用freemarker生成xml模板

    今天在java交流群里有个人问我如何用freemarker生成xml模板文件,可以手动配置参数,于是我到网上百度了一下.发现有一位同行的博文写的很nice,于是我就照着他的代码敲了一遍,最后实现了,本 ...

  3. 利用freemarker生成带fusioncharts图片的word简报

    /**  * 利用freemarker生成带fusioncharts图片的word简报  *         烟台海颐软件技术论坛  *         作者  牟云飞 新建 *         毕业 ...

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

    http://wenku.baidu.com/link?url=YxTZWVP3ssO-e_Br3LIZVq2xOQnqaSz8gLPiRUDN8NIR_wX2-Z25OqwbVn5kXqGiOFYU ...

  5. .NetCore利用Swagger生成 XML文档需要注意生成路径的地址

    发布的时候如果用 release dotnet publish --configuration release dotnet publish 默认都是debug 会出现 XML丢失问题,其实可以看下工 ...

  6. Java利用dom4j生成xml文件、解析XML

    package com.fq.fanqi; import java.io.File;import java.io.FileWriter;import java.io.IOException;impor ...

  7. 安卓开发之利用XmlSerializer生成XML文件

    package com.lidaochen.phonecall; import android.net.Uri; import android.os.Environment; import andro ...

  8. 利用freemarker+SAX解析xml的方式对excel文件字段校验

    利用freemarker对参数进行校验这篇文章主要用到的技术点: 自定义注解的使用反射机制SAX解析xmlFreemarker的运用我们在工作中经常需要上传excel文件,然后在对文件中的字段进行校验 ...

  9. 使用XML序列化器生成XML文件和利用pull解析XML文件

    首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...

随机推荐

  1. C语言实例代码

    绘制余弦曲线和直线 #include #include int main() { double y; int x,m,n,yy; for(yy=0;yy<=20;yy++) {y=0.1*yy; ...

  2. c#读写文本文档-1-用file类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  3. MapReduce 重要组件——Recordreader组件

    (1)以怎样的方式从分片中读取一条记录,每读取一条记录都会调用RecordReader类: (2)系统默认的RecordReader是LineRecordReader,如TextInputFormat ...

  4. 收藏的博客--Ogre

    1.Ogre一些东西和流程深入讲解  http://blog.csdn.net/yanonsoftware/article/category/226048 2.Ogre天龙八部分析 http://bl ...

  5. 云计算平台简介(App Engine)

    云计算平台简介(App Engine)     1   简介 App Engine: 应用程序引擎,是托管网络应用程序的云计算平台. 1.1  什么是云 云计算通常简称为“云”,是一种通过 Inter ...

  6. 在VS2010 中兼容Qt4和Qt5

    1,同时安装Qt4和Qt5 Qt_add,然后在 2. 如果之前的项目使用Qt4编写的,如果新添加新的类和ui的话,一定要选择Qt Add_in 1.1.11,不然就无法生成moc文件,随便选择 Ch ...

  7. River Crossing 简单的动态规划 .

    第一行 t  表示有几组测试数据 . 每组测试数据的 第一行是 n, m   .     然后 下面有n行数据  . 题意:有1个人和N只羊要过河.一个人单独过河花费的时间是M,每次带一只羊过河花费时 ...

  8. 学军NOIP2016模拟赛1

    GTMD这么水的一套题没有AK T1:妥妥的二分答案,贪心check. T2:问题可以转化为最长上升(还是下降我记不住了)子序列. T3:发现点被覆盖上的顺序是一定的.求出这个顺序,第一个操作在线段树 ...

  9. IndexOf() LastIndexOf() Contains() StartsWith() EndsWith()方法比较

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  10. C#使用指针表达式

    如何:获取指针变量的值 使用指针间接运算符可获取位于指针所指向的位置的变量. 表达式采用下面的形式,其中, p 是指针类型: *p; 不能对除指针类型以外的任何类型的表达式使用一元间接寻址运算符. 此 ...