当前位置:网站首页>[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
2022-07-06 00:34:00 【Programmer community】
List of articles
- One 、 Create sub tags under tag closures
- Two 、 Use MarkupBuilderHelper add to xml notes
- 3、 ... and 、 Complete code example
One 、 Create sub tags under tag closures
In the last blog 【Groovy】xml serialize ( Use MarkupBuilder Generate xml data ) Generated xml On the basis of documents , If you want it under the label , Create sub tags , As follows <team>
Create under the label <member>
label ;
<student> <name code="utf-8">Tom</name> <age>18</age> <team> <member>Jerry</member> </team></student>
In the closure that creates the tag , Nest and create labels again ;
markupBuilder.student{
// Create a sub tag under the tag team() {
member("Jerry") {
} }}
The complete code is as follows :
// Generate the above xml file markupBuilder.student{
// The format is as follows : // xml Tag name ( Label content , Tag attributes : Tag attribute value ) // Generate label content : Just write the label directly // Generate tag attributes : Tag attributes are generated using key value pairs name("Tom", code: "utf-8") {
} age(18) {
} // Create a sub tag under the tag team() {
member("Jerry") {
} }}
Two 、 Use MarkupBuilderHelper add to xml notes
In a closure, you can use mkp
obtain MarkupBuilderHelper object , This object can help add xml notes ;
/** * attribute , This property can be called from the generator closure to access helper methods , * namely {@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 Class provides some methods , among comment Method , Can be used to add comments ;
public class MarkupBuilderHelper {
private final MarkupBuilder builder; /** * Generate comments in the output . * <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 + " -->"); }}
Use in closures mkp.comment(" The student's name ")
Code , Can generate <!-- The student's name -->
notes ;
markupBuilder.student{
mkp.comment(" The student's name ")}
3、 ... and 、 Complete code example
Complete code example :
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"))// establish MarkupBuilder object // Constructor passed in FileWriter It means that you will xml Write the file into the file def markupBuilder = new MarkupBuilder(fileWriter)// Generate the above xml file markupBuilder.student{
// The format is as follows : // xml Tag name ( Label content , Tag attributes : Tag attribute value ) // Generate label content : Just write the label directly // Generate tag attributes : Tag attributes are generated using key value pairs name("Tom", code: "utf-8") {
} mkp.comment(" The student's name ") age(18) {
} mkp.comment(" Student age ") // Create a sub tag under the tag team() {
member("Jerry") {
} } mkp.comment(" Team members ")}
Execution results :
<student> <name code='utf-8'>Tom</name><!-- The student's name --> <age>18</age><!-- Student age --> <team> <member>Jerry</member> </team><!-- Team members --></student>
边栏推荐
- LeetCode 斐波那契序列
- How to solve the problems caused by the import process of ecology9.0
- Room cannot create an SQLite connection to verify the queries
- 免费的聊天机器人API
- 《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
- MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
- 图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
- Hudi of data Lake (2): Hudi compilation
- 【DesignMode】装饰者模式(Decorator pattern)
- Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
猜你喜欢
notepad++正則錶達式替換字符串
Leetcode:20220213 week race (less bugs, top 10% 555)
Determinant learning notes (I)
Extension and application of timestamp
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
AtCoder Beginner Contest 258【比赛记录】
How to make your own robot
Folding and sinking sand -- weekly record of ETF
FPGA内部硬件结构与代码的关系
小程序技术优势与产业互联网相结合的分析
随机推荐
[designmode] composite mode
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
如何制作自己的機器人
FFT learning notes (I think it is detailed)
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
孤勇者
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
devkit入门
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
【DesignMode】适配器模式(adapter pattern)
Key structure of ffmpeg -- AVCodecContext
Set data real-time update during MDK debug
数据分析思维分析方法和业务知识——分析方法(三)
Browser reflow and redraw
Hudi of data Lake (1): introduction to Hudi
Idea远程提交spark任务到yarn集群
Atcoder beginer contest 254 [VP record]
XML配置文件