FreeMarker入门 生成xml模板
生活随笔
收集整理的這篇文章主要介紹了
FreeMarker入门 生成xml模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一次寫這個,只是按著自己想的來寫,沒有什么思路。。。。
https://blog.csdn.net/kangcool_sn/article/details/85096386
?
先建一個Person對象,也可以不用,只是為了封裝數據好處理
?
public class Person { private String name; private String age; private String birthday; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }?
?
?
?
?
?
??
?
?
創建工具類 ?這邊把測試方法寫在工具類中了,可以分離出去
?
?
import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class ObjectToXmlUtil { /** * * @param xmlPath xml的路徑 * @param xmlName xml的名稱* @param code 數據編碼格式 * @param dataMap 需要渲染到xml中的map數據 * @return xmlString*/ public String createXmlFile(String xmlPath,String xmlName,String code, Map dataMap) { String result = ""; //得FreeMarker配置對象// 創建Configuration對象 Configuration cfg = new Configuration(); //設置模板編碼格式cfg.setEncoding(Locale.getDefault(),code); //得FreeMarker的關鍵對象---------模板 // 創建Template對象 Template template = null; try { // 設置FreeMarker的模版文件位置 cfg.setClassForTemplateLoading(this.getClass(),"");//cfg.setDirectoryForTemplateLoading(new File(xmlPath));template = cfg.getTemplate(xmlName); } catch (IOException e1) { e1.printStackTrace(); } template.setEncoding(code); //String path = ServletActionContext.getServletContext().getRealPath("/"); /* File dir = new File(path + htmlPath); if (!dir.exists()) { dir.mkdirs(); } */ // File fileName = new java.io.File(path + htmlPath + htmlName); //System.out.println("html file:" + fileName.getPath()); //Writer writer = null; java.io.StringWriter w =new StringWriter(); try { // 生成xmltemplate.process(dataMap, w); System.out.println(w.toString()); result = w.toString(); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } public static void main(String args[]){ObjectToXmlUtil t = new ObjectToXmlUtil();Map map =t.demo();t.createXmlFile("","Person.xml","gbk", map);}private Map demo(){Person p =new Person();p.setName("李四");p.setAge("12");p.setBirthday("20161201");p.setSex("Y");Map map = new HashMap();map.put("t", p); return map;}}?
?
?
創建自己要生成的xml模板樣式,將數據寫過來
?
?
<?xml version="1.0" encoding="gbk"?> <root id="" comment=""><person><name>${t.name}</name><age>${t.age}</age><birthday>${t.birthday}</birthday><sex>${t.sex}</sex></person> </root>?
?
最后輸出結果,轉成你想要的xml文件即可:
?
?
?
?
?
<?xml version="1.0" encoding="gbk"?> <root id="" comment=""><person><name>李四</name><age>12</age><birthday>20161201</birthday><sex>Y</sex></person> </root>?
?
?
xml模板中帶有多條的同樣處理方法
以上純屬一個小白的一點心得,勿噴
https://blog.csdn.net/kangcool_sn/article/details/85069434
?
總結
以上是生活随笔為你收集整理的FreeMarker入门 生成xml模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初学者用js是怎么敲九九乘法表的
- 下一篇: 一个发人深思的预言故事