当前位置:网站首页>[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 ;

边栏推荐
- Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
- Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
- Go learning - dependency injection
- LeetCode 6006. Take out the least number of magic beans
- [simple implementation of file IO]
- Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- 多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
- Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
- 7.5 decorator
猜你喜欢

如何制作自己的机器人

Arduino六足机器人

notepad++正则表达式替换字符串

FFT learning notes (I think it is detailed)

Browser local storage

LeetCode 1598. Folder operation log collector

Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)

XML Configuration File

Classic CTF topic about FTP protocol

KDD 2022 | 脑电AI助力癫痫疾病诊断
随机推荐
Codeforces round 804 (Div. 2) [competition record]
MySql——CRUD
Yolov5, pychar, Anaconda environment installation
MySql——CRUD
Cloud guide DNS, knowledge popularization and classroom notes
How spark gets columns in dataframe --column, $, column, apply
Reading notes of the beauty of programming
Set data real-time update during MDK debug
Search (DFS and BFS)
[simple implementation of file IO]
How to use the flutter framework to develop and run small programs
wx. Getlocation (object object) application method, latest version
如何解决ecology9.0执行导入流程流程产生的问题
Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
MDK debug时设置数据实时更新
XML Configuration File
Basic introduction and source code analysis of webrtc threads
Go learning - dependency injection
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
MySql——CRUD