KaizenSoze
KaizenSoze

Reputation: 531

Having trouble unmarshaling this xml

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

Upvotes: 2

Views: 320

Answers (1)

ANisus
ANisus

Reputation: 78045

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

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

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

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

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

Upvotes: 6

Related Questions