日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

【Groovy】xml 序列化 ( 使用 MarkupBuilder 生成 xml 数据 | 标签闭包下创建子标签 | 使用 MarkupBuilderHelper 添加 xml 注释 )

發(fā)布時(shí)間:2025/6/17 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Groovy】xml 序列化 ( 使用 MarkupBuilder 生成 xml 数据 | 标签闭包下创建子标签 | 使用 MarkupBuilderHelper 添加 xml 注释 ) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 一、標(biāo)簽閉包下創(chuàng)建子標(biāo)簽
  • 二、使用 MarkupBuilderHelper 添加 xml 注釋
  • 三、完整代碼示例





一、標(biāo)簽閉包下創(chuàng)建子標(biāo)簽



在上一篇博客 【Groovy】xml 序列化 ( 使用 MarkupBuilder 生成 xml 數(shù)據(jù) ) 生成的 xml 文件基礎(chǔ)上 , 如果要在標(biāo)簽下 , 創(chuàng)建子標(biāo)簽 , 如下的 <team> 標(biāo)簽下再創(chuàng)建 <member> 標(biāo)簽 ;

<student><name code="utf-8">Tom</name><age>18</age><team><member>Jerry</member></team> </student>

在創(chuàng)建標(biāo)簽的閉包中 , 再次嵌套創(chuàng)建標(biāo)簽即可 ;

markupBuilder.student{// 標(biāo)簽下創(chuàng)建子標(biāo)簽team() {member("Jerry") {}} }

完整代碼如下 :

// 生成上述 xml 文件 markupBuilder.student{// 格式如下 :// xml 標(biāo)簽名稱( 標(biāo)簽內(nèi)容 , 標(biāo)簽屬性 : 標(biāo)簽屬性值)// 生成標(biāo)簽內(nèi)容 : 標(biāo)簽內(nèi)容直接寫上即可// 生成標(biāo)簽屬性 : 標(biāo)簽屬性使用鍵值對(duì)方式生成name("Tom", code: "utf-8") {}age(18) {}// 標(biāo)簽下創(chuàng)建子標(biāo)簽team() {member("Jerry") {}} }



二、使用 MarkupBuilderHelper 添加 xml 注釋



在閉包中可以通過 mkp 獲取 MarkupBuilderHelper 對(duì)象 , 該對(duì)象可以幫助添加 xml 注釋 ;

/*** 屬性,該屬性可以從生成器閉包中調(diào)用以訪問幫助器方法,* 即 {@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 '&lt;!-- string --&gt;'</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>* &lt;!-- hello1 --&gt;* </pre>* while:* <pre>* comment('hello2', [:])* </pre>* will produce:* <pre>* &lt;comment&gt;hello2&lt;/comment&gt;* </pre>** @param value the text within the comment.*/public void comment(String value) {yieldUnescaped("<!-- " + value + " -->");} }

在閉包中使用 mkp.comment("學(xué)生姓名") 代碼 , 可以生成 <!-- 學(xué)生姓名 --> 注釋 ;

markupBuilder.student{mkp.comment("學(xué)生姓名") }



三、完整代碼示例



完整代碼示例 :

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"))// 創(chuàng)建 MarkupBuilder 對(duì)象 // 構(gòu)造函數(shù)中傳入 FileWriter 表示將 xml 文件寫出到文件中 def markupBuilder = new MarkupBuilder(fileWriter)// 生成上述 xml 文件 markupBuilder.student{// 格式如下 :// xml 標(biāo)簽名稱( 標(biāo)簽內(nèi)容 , 標(biāo)簽屬性 : 標(biāo)簽屬性值)// 生成標(biāo)簽內(nèi)容 : 標(biāo)簽內(nèi)容直接寫上即可// 生成標(biāo)簽屬性 : 標(biāo)簽屬性使用鍵值對(duì)方式生成name("Tom", code: "utf-8") {}mkp.comment("學(xué)生姓名")age(18) {}mkp.comment("學(xué)生年齡")// 標(biāo)簽下創(chuàng)建子標(biāo)簽team() {member("Jerry") {}}mkp.comment("小組成員") }

執(zhí)行結(jié)果 :

<student><name code='utf-8'>Tom</name><!-- 學(xué)生姓名 --><age>18</age><!-- 學(xué)生年齡 --><team><member>Jerry</member></team><!-- 小組成員 --> </student>

總結(jié)

以上是生活随笔為你收集整理的【Groovy】xml 序列化 ( 使用 MarkupBuilder 生成 xml 数据 | 标签闭包下创建子标签 | 使用 MarkupBuilderHelper 添加 xml 注释 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。