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. 一键制作u盘启动盘教程

    第一步:制作完成u深度u盘启动盘   第二步:下载Ghost Win7系统镜像文件包,存入u盘启动盘   第三步:电脑模式更改成ahci模式,不然安装完成win7系统会出现蓝屏现象 正式安装步骤: u ...

  2. Spring学习笔记之方法注入

    public  abstract class ReplacedBean {protected static final Log log = LogFactory.getLog(ReplacedBean ...

  3. Python Web 应用:WSGI基础

    在Django,Flask,Bottle和其他一切Python web 框架底层的是Web Server Gateway Interface,简称WSGI.WSGI对Python来说就像 Servle ...

  4. Python开发入门与实战1-开发环境

    1.搭建Python Django开发环境 1.1.Python运行环境安装 Python官网:http://www.python.org/ Python最新源码,二进制文档,新闻资讯等可以在Pyth ...

  5. 《JAVA学习笔记(1---13-4)》

    [1]问题: 1.什么叫做面向过程? 2.什么叫做面向对象? 解答: 1: 所谓的面向过程就是我们是一个执行者,我们要开发一个项目,这个项目要求要实现很多功能,作为执行者的我们就需要 去一个一个的找这 ...

  6. 框架设计——MVC IOC

    主要概念: 注:以下概念是自我理解,不是很准确. IOC:Inversion of Control(控制反转). 本来对象创建是通过使用类内部进行创建,现在把对象创建交给container(容器)管理 ...

  7. PAT 10-0 说反话

    我写了两种实现方法,其中第二种是参考Yomman园友的(http://www.cnblogs.com/yomman/p/4271949.html).我的方法(方法一)是用一个数组存放输入的字符串,另一 ...

  8. objectarx 卸载加载arx模块

    通常情况下,加载卸载arx模块是使用 APPLOAD命令 使用ObjectARX 代码操作,也非常简单,使用2个全局函数即可,参数为名字有扩展名 C++ int acedArxLoad( const ...

  9. 12-26 tableView的学习心得

    一:基础部分 UITableView的两种样式: 注意是只读的 1.UITableViewStytlePlain(不分组的) n 2.UITableViewStyleGrouped(分组的) 二:如何 ...

  10. Day1 summary

    对比了几篇在hadoop环境中实现关联规则.频繁项集的论文,文章结构都涉及mapreduce模型.传统与改进apriori算法比较.实验结果分析(数据规模-用时or加速比,节点-用时or加速比).有一 ...