[Freemaker基础]-- java使用freemaker生成xml

本文介绍如何使用Freemarker模板引擎生成KVM虚拟机的XML配置文件。通过创建项目、编写模板文件及测试类,实现动态生成包含指定参数的KVM配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建java project-------》freemaker,再导入jar包(freemarker.jar)
2、编写ftl文件----》kvm.ftl(内容见备注一)
3、编写测试类:TestCreate.java(内容见备注二)
4、运行测试类即可
【备注一】

<domain type='kvm'>
<name>${kvmName}</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='${centosPath}'/>
 <target dev='${vdbName}' 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>

【备注二】
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
/**
 * 
* @ClassName: TestCreate 
* @Description: 生成需要的文件数据
* @author :root
* @date 2016年5月23日 下午12:29:45 
*
 */
public class TestCreate {
private Configuration cfg;            //模版配置对象 


//初始化FreeMarker配置 
    public void init() throws Exception { 
            //创建一个Configuration实例 
            cfg = new Configuration(); 
            //设置FreeMarker的模版文件夹位置 
            cfg.setDirectoryForTemplateLoading(new File("E:\\java_project\\freemarket\\src")); 
    } 
    public void process() throws Exception { 
            //构造填充数据的Map 
        //如果有需要其他变量,在kvm.ftl中替换既可
            Map map = new HashMap(); 
            map.put("kvmName", "sxtkvm01");                                         //kvm名称
            map.put("centosPath", "CentOS-6.5-x86_64-minimal.iso");                 //所选安装系统的类型
            map.put("vdbName", "vdb2");                                             //选择的磁盘需要新加名称
            //创建模版对象 
            Template t = cfg.getTemplate("kvm.ftl");  //我自己的模块
            //在模版上执行插值操作,并输出到制定的输出流中 
            System.out.println("==============打印=========");
            t.process(map, new OutputStreamWriter(System.out,"UTF-8")); 
            //输出到本地文件中
            System.out.println("--------输出到本地磁盘----------");
            OutputStream out=new FileOutputStream(new File("E:/kvmCreate.xml"));
            t.process(map, new OutputStreamWriter(out,"UTF-8")); 
    } 
    public static void main(String[] args) throws Exception { 
    TestCreate hf = new TestCreate(); 
            hf.init(); 
            hf.process(); 
    } 
}




                

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oo寻梦in记

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值