天天看點

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

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(); 
     } 
 }