freemarker接收XML字符串

獲取xml字符串後先轉化爲 InputStream  ,在使用freemarker.ext.dom.NodeModel.parse將InputStream轉化爲freemarker中識別的xml

InputStream   in   =   new   ByteArrayInputStream(xml.getBytes("UTF-8"));

InputSource ins=new org.xml.sax.InputSource(in);

map.put("doc",freemarker.ext.dom.NodeModel.parse(ins));

或者直接加載xml文件 freemarker.ext.dom.NodeModel.parse(newFile("the/path/of/the.xml")));


可以在freemarker中使用list接收,假如xml爲

<book title=“test book”>
<para>p1.1</para>
<para>p1.2</para>
<para>p1.3</para>
</book>

<#list doc.book as book>

獲取到book的節點,獲取title ,則通過 ${book.@title}獲取

獲取book的子節點則

<#list book.para as para>

${para}直接獲取到節點

</list>

</list>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章