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

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分