当前位置:网站首页>[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 ;
边栏推荐
- Idea remotely submits spark tasks to the yarn cluster
- Calculate sha256 value of data or file based on crypto++
- Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
- Atcoder beginer contest 258 [competition record]
- The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
- Set data real-time update during MDK debug
- XML Configuration File
- Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
- KDD 2022 | 脑电AI助力癫痫疾病诊断
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
猜你喜欢
How to use the flutter framework to develop and run small programs
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
Idea远程提交spark任务到yarn集群
Spark AQE
FPGA内部硬件结构与代码的关系
Hardware and interface learning summary
LeetCode 1598. Folder operation log collector
XML Configuration File
Ffmpeg captures RTSP images for image analysis
随机推荐
Common API classes and exception systems
Determinant learning notes (I)
What is information security? What is included? What is the difference with network security?
Browser local storage
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
About the slmgr command
Mysql - CRUD
Idea remotely submits spark tasks to the yarn cluster
OpenCV经典100题
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
Hudi of data Lake (2): Hudi compilation
Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
Key structure of ffmpeg -- AVCodecContext
Codeforces round 804 (Div. 2) [competition record]
Ffmpeg learning - core module
Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
Analysis of the combination of small program technology advantages and industrial Internet
Hardware and interface learning summary
【DesignMode】组合模式(composite mode)
Opencv classic 100 questions