当前位置:网站首页>[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>
边栏推荐
- 如何解决ecology9.0执行导入流程流程产生的问题
- Uniapp development, packaged as H5 and deployed to the server
- AtCoder Beginner Contest 258【比赛记录】
- 关于slmgr命令的那些事
- Notepad + + regular expression replace String
- 常用API类及异常体系
- N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
- Codeforces Round #804 (Div. 2)【比赛记录】
- Idea remotely submits spark tasks to the yarn cluster
- Data analysis thinking analysis methods and business knowledge - analysis methods (III)
猜你喜欢
Classic CTF topic about FTP protocol
OS i/o devices and device controllers
Browser reflow and redraw
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
Comment faire votre propre robot
Arduino六足机器人
MySql——CRUD
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
Free chat robot API
Leetcode 450 deleting nodes in a binary search tree
随机推荐
FFT learning notes (I think it is detailed)
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
Set data real-time update during MDK debug
Date类中日期转成指定字符串出现的问题及解决方法
Yolov5、Pycharm、Anaconda环境安装
Go learning --- read INI file
【DesignMode】装饰者模式(Decorator pattern)
SQLServer连接数据库读取中文乱码问题解决
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
Reading notes of the beauty of programming
Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
AtCoder Beginner Contest 254【VP记录】
LeetCode 6005. The minimum operand to make an array an alternating array
MySQL存储引擎
Classic CTF topic about FTP protocol
Introduction of motor
Go learning - dependency injection
孤勇者