On Thursday, June 4, 2020 at 9:06:50 PM UTC+3, Lars R. Damerow wrote: > > Hello all, > > I need to parse XML messages from a source I don't control. Each one has > an envelope node that shows its source, and the first node in that envelope > encodes the message type. I'm trying to compose structs to make it easier > to do the parsing, and I put a stripped-down example for just one message > type on the playground here: > > https://play.golang.org/p/tszPHRM-mMO > > What's puzzling me here is that, after unmarshaling, > `v.Envelope.XMLName.Local` is empty. I'd expect it to contain "proto-alpha" > from the example XML in the source; it does pick up the version attribute > that's set on the envelope node, just not the `xml.Name` value. > > Can anyone share some clues about what I'm doing wrong here? >
You have a extra layer there in the structs, try this type Envelope struct { XMLName xml.Name Version string `xml:"version,attr"` Body GreetingBody } type GreetingBody struct { XMLName xml.Name `xml:"greeting"` Str string `xml:"str"` } func main() { var input = `<?xml version ="1.0" encoding="UTF-8"?> <proto-alpha version="1.3"> <greeting> <str>hello</str> </greeting> </proto-alpha> ` v := Envelope{} if err := xml.Unmarshal([]byte(input), &v); err != nil { log.Panic(err) } fmt.Printf("%+v\n", v) } HTH ain -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/80e1fd8d-f684-46e6-810e-ba7b02d04b4do%40googlegroups.com.