【问题标题】:Go: duplicated tags when marshalling XMLGo:编组 XML 时重复的标签
【发布时间】:2015-09-20 09:47:25
【问题描述】:

我在 Go 中为 xml 创建了结构:

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"`
}

我有两个问题:

1) 当我尝试对结构进行编码时,我会在我不期望的地方得到重复:

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

奇怪的是,验证标签没有重复。但我看不出我处理它们的方式有什么不同。

2) 我似乎找不到将命名空间放在开始标记中的方法。显然,该名称不会使用冒号。但是如果我添加一个 'xml.Name' 元素,起始标签也会被复制。

这是我在 Playground 中运行它的尝试:http://play.golang.org/p/G5NvLt-ZK7

跟进:

好的,我已经弄清楚如何通过删除定义中的“类型”来消除重复项:

type datasources struct {
      DataSource
}

但后来我失去了与之相关的属性:

<ns1:datasource>

【问题讨论】:

    标签: xml go marshalling encode


    【解决方案1】:

    您尚未添加生成的 XML 示例,但您可以通过添加 XMLName 字段并从您的 foo&gt;bar 标记中删除所有 foos 来删除重复项。

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 2015-10-11
      • 2021-10-09
      • 2019-04-30
      相关资源
      最近更新 更多