使用freemarker生成xml模板
今天在java交流群里有个人问我如何用freemarker生成xml模板文件,可以手动配置参数,于是我到网上百度了一下。发现有一位同行的博文写的很nice,于是我就照着他的代码敲了一遍,最后实现了,本想贴出他的uri的,但是找不到了。网上这样的例子很多,大家可以自己到网上看看,我的建议是一个问题多看几个解决方案,这样才能找到正确的或者是最优解。
其实要实现的功能就是简单的将红框中的内容简单替换
下面是java代码:
package createxml;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;public class XMLTest { private Configuration configuration = null; public XMLTest(){ configuration = new Configuration(); configuration.setDefaultEncoding("UTF-8"); } public static void main(String[] args) { XMLTest test = new XMLTest(); test.createWord(); } public void createWord(){ Map<String,Object> dataMap=new HashMap<String,Object>(); getData(dataMap); configuration.setClassForTemplateLoading(this.getClass(), "/createxml"); //FTL文件所存在的位置,我的只能放在与java相同的包下 Template t=null; try { t = configuration.getTemplate("xmltemp.ftl"); //文件名 } catch (IOException e) { e.printStackTrace(); } File outFile = new File("G:/outFilessa"+Math.random()*10000+".xml"); //生成文件的路径 Writer out = null; try { out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { t.process(dataMap, out); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //这里赋值的时候需要注意,xml中需要的数据你必须提供给它,不然会报找不到某元素错的. private void getData(Map<String, Object> dataMap) { dataMap.put("domaintype", "mytype"); }
}
然后是模板文件:
<domain type='${domaintype}'> <name>bjsxtvm03</name><memory unit='KiB'>1048576</memory><currentMemory unit='KiB'>1048576</currentMemory><vcpu placement='static'>1</vcpu><os><type arch='x86_64' machine='rhel6.6.0'>hvm</type><boot dev='hd'/></os><features><acpi/><apic/><pae/></features><clock offset='utc'/><on_poweroff>destroy</on_poweroff><on_reboot>restart</on_reboot><on_crash>restart</on_crash><devices><emulator>/usr/libexec/qemu-kvm</emulator><disk type='file' device='disk'><driver name='qemu' type='qcow2' cache='none'/><source file='/kvmtest/centos-6.6-04.qcow2'/><target dev='vda' bus='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/></disk><disk type='block' device='cdrom'><driver name='qemu' type='raw'/><target dev='hdc' bus='ide'/><readonly/><address type='drive' controller='0' bus='1' target='0' unit='0'/></disk><controller type='usb' index='0' model='ich9-ehci1'><address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/></controller><controller type='usb' index='0' model='ich9-uhci1'><master startport='0'/><address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/></controller><controller type='usb' index='0' model='ich9-uhci2'><master startport='2'/><address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/></controller><controller type='usb' index='0' model='ich9-uhci3'><master startport='4'/><address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/></controller><controller type='ide' index='0'><address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/></controller><interface type='network'><source network='default'/><model type='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/></interface><serial type='pty'><target port='0'/></serial><console type='pty'><target type='serial' port='0'/></console><input type='tablet' bus='usb'/><input type='mouse' bus='ps2'/><graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'><listen type='address' address='0.0.0.0'/></graphics><video><model type='cirrus' vram='9216' heads='1'/><address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/></video><memballoon model='virtio'><address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/></memballoon></devices>
</domain>
这里要说明几点的是:
1,我自己测试的时候只能将模板文件放在java的包下,也就是和java同一个地方。
使用freemarker生成xml模板相关推荐
- freemarker生成word模板
freemarker生成word模板 一.引入依赖 <dependency><groupId>org.freemarker</groupId><artifac ...
- FreeMarker入门 生成xml模板
第一次写这个,只是按着自己想的来写,没有什么思路.... https://blog.csdn.net/kangcool_sn/article/details/85096386 先建一个Person对象 ...
- 用freemarker生成word模板
需求: 给文书统一生成一个搞头文件,文件内容基本是一样的. 用freemarker生成docx文档 一.生成一个docx结尾的word模板,然后把文档的后缀docx改成zip,zip里会有生成很多文件 ...
- Freemarker 生成word 模板文件
2019独角兽企业重金招聘Python工程师标准>>> /*** * word 下载生成 * @param positionvo * @return ...
- Java实现freemarker生成word模板
博主在做的是将word源文件中的内容读取出来,然后按照固定的格式输出,当然,源文件有大量,所以编写了程序来批量自动生成目标文件. 本文要介绍的就是如何生成模板,并将从源文件截取的内容填充到模板文件(即 ...
- 使用IText5+Freemarker生成PDF(模板可以使用CCS3分页效果)
iText介绍 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...
- freemarker 的xml模板
public String xmlFormwork(String xmlPath, String xmlName, String format, Map<String, Object> d ...
- 生成xml_freemarker快速生成xml文件
记录一次用freeMarker生成xml文件 import freemarker.template.Configuration;import freemarker.template.Template; ...
- JAVA通过xml模板生成DOCX文档
准备工作: 1.xml模版,获取方式为docx文件winrar方式打开,取出document.xml 2.将内容格式化后修改需要替换的内容为freemarker标签,如下: 3.文件准备好后存放到某个 ...
最新文章
- 万年历小程序(黑框)
- 5、Scala模式匹配
- oracle apply handler,做什么BaseHandler.apply_response_修复在django吗?
- 计划将项目中使用entity framework的要点记录到改栏目下
- 如何实现自己的机器做服务器
- python你是否也走进了这个误区了_Python是不是被严重高估了?
- Java知识点总结(注解-内置注解)
- luogu_1984 [SDOI2008]烧水问题
- C#语言: 生成静态页面核心代码
- Codeblocks中搭建Qt环境遇到个问题
- 两个WebUserControl之间传值
- IOS开发-jqeurey mobile
- AP注册不上线(现网中存在其它厂商的控制器)
- 技术分享 | 相较于ROS 1,ROS 2有哪些优越性?
- 【笑爆肚子的超级冷笑话】
- 生信:1:vcf格式文件解读
- HTTP协议入门 状态码大全
- 第一门编程语言选谁?
- 一种很欣赏的操盘手法
- (MATLAB与数学建模习题)不一样的十六进制
热门文章
- python 速度 memmap_浅析Python 读取图像文件的性能对比
- android java和c混合编程_C/C++在Java项目、Android和Objective-C三大平台下实现混合编程...
- oracle 查询每组条数,Oracle SQL查询:根据时间检索每组的最新值
- 《化工原理》基本知识点
- java normalize_java – XPath normalize-space()返回一系列规范化字符串
- Vue 学习第四天--第一部分 --盲点整理与昨天知识回顾
- 直播预告 | TCCI追问研讨会第二期:双向脑机接口如何实现?
- 做科研没人带,发不了文章怎么办?
- 直播 | WWW 2021:用先验知识指导BERT注意力机制的语义文本匹配
- 商汤2021春季校园招聘正式启动!“春招百晓生”系列空宣强势来袭