1. 下载groovyall-1.6.4.jar,加入工程中
2. 写template 文件
<WholeMsg MsgMark='120' Priority='' FieldNum=''> <person> <id>${id}</id> <name>${name}</name> <salary>${salary}</salary> </person> </WholeMsg>
3. 代码
import groovy.text.SimpleTemplateEngine; import groovy.text.Template; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.codehaus.groovy.control.CompilationFailedException; public class DroolsTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub File file=new File("D:\\StudyCode\\checkDrools\\src\\template.tmpl"); Template tmpl =null; try { tmpl = new SimpleTemplateEngine().createTemplate(file); } catch (CompilationFailedException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Map binding = new HashMap(); //binding.put("name", "美女"); binding.put("id", "101"); binding.put("name", "101"); binding.put("salary", "50000"); String xml = tmpl.make(binding).toString(); System.out.println(xml); } }
4. 输出
<WholeMsg MsgMark='120' Priority='' FieldNum=''> <person> <id>101</id> <name>101</name> <salary>50000</salary> </person> </WholeMsg>