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

List of articles

  • One 、 Use MarkupBuilder Generate xml data
  • Two 、 Complete code example

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 ;

 Insert picture description here

原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140223050603.html