在解组此xml时遇到麻烦 [英] Having trouble unmarshaling this xml

查看:52
本文介绍了在解组此xml时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to understand how to unmarshall XML in Go. Read through multiple examples and stackoverflow questions. What I want is a slice with the all the patches installed on the system. I can't even get the patches to unmarshal, no errors, just an empty slice. Probably doing something basically wrong, thanks in advance for any suggestions.

<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 包未填充未导出的字段.xml文档说:

The problem I believe you have is the xml package not populating unexported fields. The xml documentation says:

因为Unmarshal使用了反射包,所以它只能分配给导出的(大写)字段.

Because Unmarshal uses the reflect package, it can only assign to exported (upper case) fields.

您需要做的就是将 group 更改为 Group :

All you need to do is to change group to Group:

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

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

You have a working example here: http://play.golang.org/p/koSzZr-Bdn

这篇关于在解组此xml时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆