amracel
amracel

Reputation: 333

Go: duplicated tags when marshalling XML

I've created structs for xml in Go:

type ExceptionSorter struct {
    ExceptionClass string `xml:"class-name,attr"`
}

type ValidConnection struct {
    ConnClass string  `xml:"class-name,attr"`
}

type Validation struct {
    ValidConnection ValidConnection `xml:"ns1:valid-connection-checker"`
    ExceptionSorter ExceptionSorter `xml:"ns1:exception-sorter"`
}

type DataSource struct {
    Jta         bool     `xml:"jta,attr"`
    JndiName    string   `xml:"jndi-name,attr"`
    PoolName    string   `xml:"pool-name,attr"`
    Enabled     bool     `xml:"enabled,attr"`
    JavaContext bool     `xml:"use-java-context,attr"`
    Spy         bool     `xml:"spy,attr"`
    UseCcm      bool     `xml:"use-ccm,attr"`
    Connect     string   `xml:"ns1:datasource>ns1:connection-url"`
    Class       string   `xml:"ns1:datasource>ns1:driver-class"`
    Driver      string   `xml:"ns1:datasource>ns1:driver"`
    MinPool     int      `xml:"ns1:datasource>ns1:pool>min-pool-size"`
    MaxPool     int      `xml:"ns1:datasource>ns1:pool>max-pool-size"`
    SecUser     string   `xml:"ns1:datasource>ns1:security>ns1:user-name"`
    SecPw       string   `xml:"ns1:datasource>ns1:security>ns1:password"`
    Validation  Validation `xml:"ns1:datasource>ns1:validation"`
    Timeout     string    `xml:"ns1:datasource>ns1:timeout,omitempty"`
    Statement   string    `xml:"ns1:datasource>ns1:statement,omitempty"`
}

type DataSources struct {
    XmlName     string `xml:"xmlns:xsi,attr"`
    XmlNs       string `xml:"xmlns:ns1,attr"`
    SchemaLocn  string `xml:"xsi:schemaLocation,attr"`
    DataSource  DataSource `xml:"ns1:datasource"`
}

And I'm having two issues with it:

1) When I try to encode the structure I get duplicates where I'm not expecting them:

    <DataSources ....>
       <ns1:datasource ....>
         <ns1:datasource>

Oddly, the Validation tag is not duplicated. But I can't see the difference in the way I'm handling them.

2) I can't seem to find a way to put the namespace in the beginning tag. The name, obviously, won't take a colon. But if I add an 'xml.Name' element, the starting tag is duplicated as well.

Here's my attempts to run it in the Playground: http://play.golang.org/p/G5NvLt-ZK7

FOLLOW-UP:

OK, I've figured out how to get rid of the duplicate, by removing the 'type' in the definition:

type datasources struct {
      DataSource
}

But then I lose the attributes associated with it:

<ns1:datasource>

Upvotes: 3

Views: 885

Answers (1)

Ainar-G
Ainar-G

Reputation: 36189

You haven't added the example of the resulting XML should look like, but you can remove duplication by adding an XMLName field and removing all foos from your foo>bar tags.

type DataSource struct {
    XMLName     xml.Name   `xml:"ns1 ns1:datasource"`
    // ...
    Connect     string     `xml:"ns1:connection-url"`
    Class       string     `xml:"ns1:driver-class"`
    Driver      string     `xml:"ns1:driver"`
    MinPool     int        `xml:"ns1:pool>min-pool-size"`
    MaxPool     int        `xml:"ns1:pool>max-pool-size"`
    SecUser     string     `xml:"ns1:security>ns1:user-name"`
    SecPw       string     `xml:"ns1:security>ns1:password"`
    Validation  Validation `xml:"ns1:validation"`
    Timeout     string     `xml:"ns1:timeout,omitempty"`
    Statement   string     `xml:"ns1:statement,omitempty"`
}

http://play.golang.org/p/7MDsb_UMjg

Upvotes: 1

Related Questions