当前位置:网站首页>[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
2022-07-06 00:34:00 【Programmer community】
One 、 Use MarkupBuilder Generate xml data
Generate
<student> <name code="utf-8">Tom</name> <age>18</age></student>Styling xml file ,
First , establish FileWriter object , Then used to generate xml Data output to file ;
def fileWriter = new FileWriter(new File("a.xml"))then , establish MarkupBuilder object , Constructor passed in FileWriter It means that you will xml Write the file into the file ;
// establish MarkupBuilder object // Constructor passed in FileWriter It means that you will xml Write the file into the file def markupBuilder = new MarkupBuilder(fileWriter)Last , Generate xml data ;
// 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) {
}}Here's a detailed introduction xml Data generation process ,
markupBuilder.student Express , Generated xml The root node of the data is <student></student> ,
xml In the data label The generation format is as follows :
xml Tag name ( Label content , Tag attributes : Tag attribute value )Generate label content : The label content can be written directly in brackets ;
age(18){}Code can be generated<age>18</age>Content ;Generate tag attributes : Tag attributes are generated using key value pairs ;
name("Tom", code: "utf-8") {}Code can be generated<name code="utf-8">Tom</name>Content ;
Two 、 Complete code example
Complete code example :
import groovy.xml.MarkupBuilder"""<student> <name code="utf-8">Tom</name> <age>18</age></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") {
} age(18) {
}}Generated xml The documents are as follows :
<student> <name code='utf-8'>Tom</name> <age>18</age></student>The following figure , On the right side of the a.xml That's generated xml file ;

边栏推荐
猜你喜欢

Ffmpeg learning - core module

FFmpeg抓取RTSP图像进行图像分析

anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败

How to solve the problems caused by the import process of ecology9.0

建立时间和保持时间的模型分析

Folding and sinking sand -- weekly record of ETF

FFT learning notes (I think it is detailed)

Free chat robot API

OpenCV经典100题

Determinant learning notes (I)
随机推荐
【线上小工具】开发过程中会用到的线上小工具合集
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Room cannot create an SQLite connection to verify the queries
Extracting profile data from profile measurement
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
Spark SQL null value, Nan judgment and processing
Hudi of data Lake (2): Hudi compilation
FFmpeg抓取RTSP图像进行图像分析
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
【文件IO的简单实现】
Browser local storage
Introduction of motor
Notepad++ regular expression replacement string
STM32 configuration after chip replacement and possible errors
Codeforces Round #804 (Div. 2)【比赛记录】
notepad++正则表达式替换字符串
Reading notes of the beauty of programming
[Online gadgets] a collection of online gadgets that will be used in the development process
Go learning --- structure to map[string]interface{}
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)