Go: duplicate tags when sorting XML

I created structures 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 am having two problems:

1) When I try to code a structure I get duplicates where I don't expect them:

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

      

Oddly enough, the Validation tag is not duplicated. But I see no difference in how I handle them.

2) I cannot find a way to put the namespace at the beginning of the tag. The title obviously won't accept a colon. But if I add the "xml.Name" element, the start tag is duplicated as well.

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

FOLLOW-UP:

OK, I 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>

      

+3


source to share


1 answer


You haven't added an example of the resulting XML, but you can remove duplication by adding a field XMLName

and removing everything foo

from the tags foo>bar

.

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

+1


source







All Articles