当前位置:网站首页>[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 ;
边栏推荐
- MySQL之函数
- State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
- Go learning - dependency injection
- 如何利用Flutter框架开发运行小程序
- Spark AQE
- Cloud guide DNS, knowledge popularization and classroom notes
- An understanding of & array names
- Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
- Hudi of data Lake (1): introduction to Hudi
- 如何制作自己的機器人
猜你喜欢
Folding and sinking sand -- weekly record of ETF
Classic CTF topic about FTP protocol
关于slmgr命令的那些事
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
如何利用Flutter框架开发运行小程序
2022-02-13 work record -- PHP parsing rich text
How to solve the problems caused by the import process of ecology9.0
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
Spark AQE
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
随机推荐
uniapp开发,打包成H5部署到服务器
Go learning --- read INI file
Determinant learning notes (I)
Basic introduction and source code analysis of webrtc threads
Ffmpeg learning - core module
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
Reading notes of the beauty of programming
synchronized 和 ReentrantLock
Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
Classic CTF topic about FTP protocol
Folding and sinking sand -- weekly record of ETF
Opencv classic 100 questions
Cloud guide DNS, knowledge popularization and classroom notes
数据分析思维分析方法和业务知识——分析方法(三)
OpenCV经典100题
建立时间和保持时间的模型分析
FFmpeg抓取RTSP图像进行图像分析
Classical concurrency problem: the dining problem of philosophers
Yolov5、Pycharm、Anaconda环境安装
免费的聊天机器人API