dongshuzui0335 2015-07-02 18:14
浏览 60
已采纳

转到:编组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>
  • 写回答

1条回答 默认 最新

  • duanmiyang6201 2015-07-29 15:54
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来