【问题标题】:Having trouble unmarshaling this xml解组此 xml 时遇到问题
【发布时间】:2012-11-16 04:47:57
【问题描述】:

试图了解如何在 Go 中解组 XML。通读多个示例和 stackoverflow 问题。我想要的是一个包含系统上安装的所有补丁的切片。我什至无法将补丁解组,没有错误,只是一个空切片。可能做的事情基本上是错误的,提前感谢您的任何建议。

<probe version="1.3" date="2012-03-26:17:10">
     <properties>
     </properties>
     <patches group="server">
        <file name="5002012-02-09CR00000server.jar"/>
        <file name="5002012-02-17CR00001server.jar"/>
     </patches>
     <patches group="client">
        <file name="5002012-02-09CR00000client.jar"/>
        <file name="5002012-02-17CR00001client.jar"/>
     </patches>
</probe>
type Patch struct {
    group string `xml:"group,attr"`
}

type Probe struct {
    XMLName xml.Name `xml"probe"`
    Patches []Patch `xml:"patches"`
}

【问题讨论】:

    标签: xml go


    【解决方案1】:

    我相信您遇到的问题是 xml 包没有填充未导出的字段。 xml 文档说:

    由于 Unmarshal 使用反射包,它只能分配给导出的(大写)字段。

    您只需将group 更改为Group

    type Patch struct { Group string `xml:"group,attr"` }
    

    您在这里有一个工作示例: http://play.golang.org/p/koSzZr-Bdn

    【讨论】:

    • 我希望我能给你更多的投票。这就是问题所在,我尝试了太多变体,一路上打错了字。
    • 很容易发生。我自己从来没有使用过xml,但是json 也有类似的问题,它也使用了反射。很高兴为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多