【Groovy】xml 序列化 ( 使用 MarkupBuilder 生成 xml 数据 | 标签闭包下创建子标签 | 使用 MarkupBuilderHelper 添加 xml 注释 )
生活随笔
收集整理的這篇文章主要介紹了
【Groovy】xml 序列化 ( 使用 MarkupBuilder 生成 xml 数据 | 标签闭包下创建子标签 | 使用 MarkupBuilderHelper 添加 xml 注释 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、標簽閉包下創建子標簽
- 二、使用 MarkupBuilderHelper 添加 xml 注釋
- 三、完整代碼示例
一、標簽閉包下創建子標簽
在上一篇博客 【Groovy】xml 序列化 ( 使用 MarkupBuilder 生成 xml 數據 ) 生成的 xml 文件基礎上 , 如果要在標簽下 , 創建子標簽 , 如下的 <team> 標簽下再創建 <member> 標簽 ;
<student><name code="utf-8">Tom</name><age>18</age><team><member>Jerry</member></team> </student>在創建標簽的閉包中 , 再次嵌套創建標簽即可 ;
markupBuilder.student{// 標簽下創建子標簽team() {member("Jerry") {}} }完整代碼如下 :
// 生成上述 xml 文件 markupBuilder.student{// 格式如下 :// xml 標簽名稱( 標簽內容 , 標簽屬性 : 標簽屬性值)// 生成標簽內容 : 標簽內容直接寫上即可// 生成標簽屬性 : 標簽屬性使用鍵值對方式生成name("Tom", code: "utf-8") {}age(18) {}// 標簽下創建子標簽team() {member("Jerry") {}} }二、使用 MarkupBuilderHelper 添加 xml 注釋
在閉包中可以通過 mkp 獲取 MarkupBuilderHelper 對象 , 該對象可以幫助添加 xml 注釋 ;
/*** 屬性,該屬性可以從生成器閉包中調用以訪問幫助器方法,* 即 {@link MarkupBuilderHelper#yield(String)},* {@link MarkupBuilderHelper#yieldUnescaped(String)},* {@link MarkupBuilderHelper#pi(Map)},* {@link MarkupBuilderHelper#xmlDeclaration(Map)} and* {@link MarkupBuilderHelper#comment(String)}.** @return this MarkupBuilder*/public MarkupBuilderHelper getMkp() {return new MarkupBuilderHelper(this);}MarkupBuilderHelper 類中提供了一些方法 , 其中 comment 方法 , 可以用于添加注釋 ;
public class MarkupBuilderHelper {private final MarkupBuilder builder;/*** 在輸出中生成注釋。* <p>* <code>mkp.comment 'string'</code> is equivalent to* <code>mkp.yieldUnescaped '<!-- string -->'</code>.* To create an element with the name 'comment', you need* to supply empty attributes, e.g.:* <pre>* comment('hello1')* </pre>* or* <pre>* mkp.comment('hello1')* </pre>* will produce:* <pre>* <!-- hello1 -->* </pre>* while:* <pre>* comment('hello2', [:])* </pre>* will produce:* <pre>* <comment>hello2</comment>* </pre>** @param value the text within the comment.*/public void comment(String value) {yieldUnescaped("<!-- " + value + " -->");} }在閉包中使用 mkp.comment("學生姓名") 代碼 , 可以生成 <!-- 學生姓名 --> 注釋 ;
markupBuilder.student{mkp.comment("學生姓名") }三、完整代碼示例
完整代碼示例 :
import groovy.xml.MarkupBuilder""" <student><name code="utf-8">Tom</name><age>18</age><team><member>Jerry</member></team> </student> """def fileWriter = new FileWriter(new File("a.xml"))// 創建 MarkupBuilder 對象 // 構造函數中傳入 FileWriter 表示將 xml 文件寫出到文件中 def markupBuilder = new MarkupBuilder(fileWriter)// 生成上述 xml 文件 markupBuilder.student{// 格式如下 :// xml 標簽名稱( 標簽內容 , 標簽屬性 : 標簽屬性值)// 生成標簽內容 : 標簽內容直接寫上即可// 生成標簽屬性 : 標簽屬性使用鍵值對方式生成name("Tom", code: "utf-8") {}mkp.comment("學生姓名")age(18) {}mkp.comment("學生年齡")// 標簽下創建子標簽team() {member("Jerry") {}}mkp.comment("小組成員") }執行結果 :
<student><name code='utf-8'>Tom</name><!-- 學生姓名 --><age>18</age><!-- 學生年齡 --><team><member>Jerry</member></team><!-- 小組成員 --> </student>總結
以上是生活随笔為你收集整理的【Groovy】xml 序列化 ( 使用 MarkupBuilder 生成 xml 数据 | 标签闭包下创建子标签 | 使用 MarkupBuilderHelper 添加 xml 注释 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Groovy】xml 序列化 ( 使用
- 下一篇: 【Groovy】Xml 反序列化 ( 使