Table of Contents
Correct answer
Home Backend Development Golang Golang xml key name conflict

Golang xml key name conflict

Feb 05, 2024 pm 10:03 PM

Golang xml 键名称冲突

Question content

I need to generate a complex xml file in order to load it into another system. Therefore, tags and tag order cannot be changed as they must follow a precise format to load correctly. I'm currently using the encoding/xml package in golang to try to accomplish this.

The problem I'm having is that I can't have two tags with the same name. I get the following error: main.XMLDict Field "Key1" with label "key" conflicts with field "Key2" with label "key". Below is my simplified code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

import (

    "fmt"

    "encoding/xml"

    "os"

)

 

type XMLDict struct {

    XMLName xml.Name    `xml:"dict"`

    Key1    string      `xml:"key"`

    Data1   string      `xml:"data"`

    Key2    string      `xml:"key"`

    StringArray XMLStringArray

}

 

type XMLStringArray struct {

    XMLName  xml.Name    `xml:"array"̀`

    XMLString   []string `xml:"string"`

}

 

func main() {

 

    sa := make([]string, 3)

    sa[0] = "g"

    sa[1] = "h"

    sa[2] = "i"

 

    arr := XMLStringArray{

        XMLString: sa,

    }

 

    master := XMLDict{

        Key1: "Color",

        Data1: "Random data",

        Key2: "Curve",

        StringArray: arr,

    }

 

    output, err := xml.MarshalIndent(master, "  ", "    ")

    if err != nil {

        fmt.Printf("error: %v\n", err)

    }

 

    os.Stdout.Write(output)

 

    return

}

Copy after login

Here is an example of the XML file I need to generate:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

<dict>

            <key>Color</key>

            <data>

            BAtzdHJlYW10eXBlZIHoA4QBQISEhAdOU0NvbG9yAISECE5TT2Jq

            ZWN0AIWEAWMChARmZmZmg7oehT2DZmYmP4NmZiY/AYY=

            </data>

            <key>Curve</key>

            <dict>

                <key>Interpolation</key>

                <integer>5</integer>

                <key>Points</key>

                <array>

                    <string>{0, 0}</string>

                    <string>{0.05, 0.053871}</string>

                    <string>{0.1, 0.110555}</string>

                    <string>{0.15, 0.166793}</string>

                    <string>{0.2, 0.216919}</string>

                    <string>{0.3, 0.327703}</string>

                    <string>{0.4, 0.440897}</string>

                    <string>{0.5, 0.539322}</string>

                    <string>{0.6, 0.657477}</string>

                    <string>{0.7, 0.763339}</string>

                    <string>{0.75, 0.814082}</string>

                    <string>{0.8, 0.861097}</string>

                    <string>{0.85, 0.904147}</string>

                    <string>{0.9, 0.944079}</string>

                    <string>{0.95, 0.974036}</string>

                    <string>{0.98, 0.990085}</string>

                    <string>{1, 1}</string>

                </array>

            </dict>

            <key>Line Width</key>

            <real>0.0040000001899898052</real>

            <key>Points Diameter</key>

            <real>0.014999999664723873</real>

            <key>Precision</key>

            <real>9.9999997473787516e-05</real>

        </dict>

Copy after login

So, my questions: 1) Is there an easy way to solve this problem, still using the structural approach of building the data and then generating the XML, 2) Is there a better way to build and generate this file than using Stuts or 3) Am I just doing something wrong?

EDIT: Below is the requested input file:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

"File created by Curve"

LGOROWLENGTH 3

#

# Curve4 Run Information

#

# Run: Run 1 - Calibration

# Based On: <none>

#

# Values in this file are 'Wanted'

#

# Gray Balance: On

#    reduced after 100%

#    until 100%

#    media white values: Measured

#    media aim points: -0.17 / 0.96

#

NUMBER_OF_FIELDS    6

BEGIN_DATA_FORMAT

SampleID    SAMPLE_NAME CMYK_C  CMYK_M  CMYK_Y  CMYK_K

END_DATA_FORMAT

NUMBER_OF_SETS 17

BEGIN_DATA

A0  "0.00"  0.0000  0.0000  0.0000  0.0000

A1  "5.00"  5.3871  5.1408  5.2761  5.5775

A2  "10.00" 11.0555 10.7418 10.6026 11.0702

A3  "15.00" 16.6793 16.3894 15.8763 16.7064

A4  "20.00" 21.6919 21.7968 21.0234 22.3956

A5  "30.00" 32.7703 32.7458 31.1841 33.6738

A6  "40.00" 44.0897 42.4801 40.0887 44.2966

A7  "50.00" 53.9322 54.2364 49.1680 54.8968

A8  "60.00" 65.7477 64.9423 59.1719 66.4696

A9  "70.00" 76.3339 75.4398 69.4943 77.1661

A10 "75.00" 81.4082 80.5957 75.1423 82.0637

A11 "80.00" 86.1097 84.9296 81.1298 86.7399

A12 "85.00" 90.4147 88.8039 86.7945 90.8142

A13 "90.00" 94.4079 92.6783 92.1110 94.2426

A14 "95.00" 97.4036 96.2097 96.6019 97.2088

A15 "98.00" 99.0085 98.5775 98.7747 98.9000

A16 "100.00"    100.0000    100.0000    100.0000    100.0000

END_DATA

Copy after login


Correct answer


I think there are many solutions to achieve this using XML custom marshaler; this is my attempt.

First of all, in your root <dict> it always seems to be a "list" of items like this:

1

2

3

4

5

<key>a-string</key>

<data|integer|real|array|dict>a-value</data|integer|real|array|dict>

 

<key>another-string</key>

<data|integer|real|array|dict>another-value</data|integer|real|array|dict>

Copy after login

To represent each of these, we can define a KeyValue structure, where Value is just an interface to represent anything it can hold (data , integer,...)

1

2

3

4

5

6

7

8

type KeyValue struct {

    Key   string

    Value Value

}

 

type Value interface {

    getXMLName() string // this will return data, integer, array, etc, depending on the implementation

}

Copy after login

We can then implement Value using the different types provided in the example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

type ValueData struct {

    Data string `xml:",chardata"`

}

 

func (v ValueData) getXMLName() string { return "data" }

 

type ValueInteger struct {

    Integer int `xml:",chardata"`

}

 

func (v ValueInteger) getXMLName() string { return "integer" }

 

type ValueStringArray struct {

    Array []string `xml:"string"`

}

 

func (v ValueStringArray) getXMLName() string { return "array" }

 

type ValueReal struct {

    Real float64 `xml:",chardata"`

}

 

func (v ValueReal) getXMLName() string { return "real" }

Copy after login

Within the XML tag, chardata is used to avoid including extra levels in our XML document; for ValueStringArray, we use string because your The project is named this way.

Finally, to allow recursion (dict internal dict), we can also define this structure:

1

2

3

4

5

6

type ValueDict struct {

    XMLName    xml.Name `xml:"dict"`

    KeysValues []KeyValue

}

 

func (v ValueDict) getXMLName() string { return "dict" }

Copy after login

After completing all operations, we can implement a custom marshaler:

1

2

3

4

5

6

7

8

9

func (kv KeyValue) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {

    if err := e.EncodeElement(kv.Key, xml.StartElement{Name: xml.Name{Local: "key"}}); err != nil {

        return err

    }

    if err := e.EncodeElement(kv.Value, xml.StartElement{Name: xml.Name{Local: kv.Value.getXMLName()}}); err != nil {

        return err

    }

    return nil

}

Copy after login

This will allow the generation of projects like this:

1

2

<key>Line Width</key>

<real>0.004000000189989805</real>

Copy after login
The name of the

tag (here real) is based on what is returned by the getXMLName() method.

Finally, you can define Go structs and marshals like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

master := ValueDict{

    KeysValues: []KeyValue{

        {

            Key: "Color",

            Value: ValueData{

                Data: "BAtzdHJlYW10eXBlZIHoA4QBQISEhAdOU0NvbG9yAISECE5TT2JqZWN0AIWEAWMChARmZmZmg7oehT2DZmYmP4NmZiY/AYY=",

            },

        },

        {

            Key: "Curve",

            Value: ValueDict{

                KeysValues: []KeyValue{

                    {

                        Key: "Interpolation",

                        Value: ValueInteger{

                            Integer: 5,

                        },

                    },

                    {

                        Key: "Points",

                        Value: ValueStringArray{

                            Array: []string{

                                "{0, 0}",

                                "{0.05, 0.053871}",

                                "{0.1, 0.110555}",

                                "{0.15, 0.166793}",

                                "{0.2, 0.216919}",

                                "{0.3, 0.327703}",

                                "{0.4, 0.440897}",

                                "{0.5, 0.539322}",

                                "{0.6, 0.657477}",

                                "{0.7, 0.763339}",

                                "{0.75, 0.814082}",

                                "{0.8, 0.861097}",

                                "{0.85, 0.904147}",

                                "{0.9, 0.944079}",

                                "{0.95, 0.974036}",

                                "{0.98, 0.990085}",

                                "{1, 1}",

                            },

                        },

                    },

                },

            },

        },

        {

            Key: "Line Width",

            Value: ValueReal{

                Real: 0.0040000001899898052,

            },

        },

        {

            Key: "Points Diameter",

            Value: ValueReal{

                Real: 0.014999999664723873,

            },

        },

        {

            Key: "Precision",

            Value: ValueReal{

                Real: 9.9999997473787516e-05,

            },

        },

    },

}

 

output, err := xml.MarshalIndent(master, "  ", "    ")

if err != nil {

    fmt.Printf("error: %v\n", err)

}

 

os.Stdout.Write(output)

Copy after login

This will print exactly what is in the sample XML provided in your question.

You can test the complete code in the Go Playground: https://go.dev/play/p /k8cEIywx3UB.

The main advantages of this solution are:

  • We just need to implement a custom XML marshaler
  • It can be easily extended if you have other structures in your XML document (let's say a boolean value, we can create a ValueBool implementation Value)

The above is the detailed content of Golang xml key name conflict. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

PostgreSQL monitoring method under Debian PostgreSQL monitoring method under Debian Apr 02, 2025 am 07:27 AM

This article introduces a variety of methods and tools to monitor PostgreSQL databases under the Debian system, helping you to fully grasp database performance monitoring. 1. Use PostgreSQL to build-in monitoring view PostgreSQL itself provides multiple views for monitoring database activities: pg_stat_activity: displays database activities in real time, including connections, queries, transactions and other information. pg_stat_replication: Monitors replication status, especially suitable for stream replication clusters. pg_stat_database: Provides database statistics, such as database size, transaction commit/rollback times and other key indicators. 2. Use log analysis tool pgBadg

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

How to optimize Debian Hadoop How to optimize Debian Hadoop Apr 02, 2025 am 08:54 AM

To improve the performance of DebianHadoop cluster, we need to start from hardware, software, resource management and performance tuning. The following are some key optimization strategies and suggestions: 1. Select hardware and system configurations carefully to select hardware configurations: Select the appropriate CPU, memory and storage devices according to actual application scenarios. SSD accelerated I/O: Use solid state hard drives (SSDs) as much as possible to improve I/O operation speed. Memory expansion: Allocate sufficient memory to NameNode and DataNode nodes to cope with larger data processing and tasks. 2. Software configuration optimization Hadoop configuration file adjustment: core-site.xml: Configure HDFS default file system

See all articles