xml.Unmarshal how to get struct of xml?

https://play.golang.org/p/PEu0kV5WW36

From the encoding/xml Unmarshal documentation:

  • If the XMLName field has an associated tag of the form
    “name” or “namespace-URL name”, the XML element must have
    the given name (and, optionally, name space) or else Unmarshal
    returns an error.

https://play.golang.org/p/56o9clh20n7

1 Like

thx

why RspEnvelope.XMLName tag is xml:"Envelope" not xml:"soap:Envelope"

Perhaps it should be! I took the example you posted and replaced the “short” XML namespace declarations with their full definitions. RspEnvelope.XMLName didn’t have a namespace, so when I did a replace, I didn’t catch it.

// RspEnvelope is a SOAP envelope.
type RspEnvelope struct {
	XMLName      xml.Name      `xml:"Envelope"`

If it should be in a namespace, I suggest putting it in one similarly to how I replaced the other namespaces.

Often on this forum, I’m just trying to serve as “another set of eyes” in an attempt to help both the one asking the question and myself become better programmers!

how to get envelope.Body https://play.golang.org/p/UK2ghMF0Wr-

You can’t put the short XML names in the struct tags like

`xml:"soap:Header"`

You have to put the full namespace URL:

`xml:"http://schemas.xmlsoap.org/soap/envelope/ Header"`

https://play.golang.org/p/AiuF95Qnamf

why this still work ok.

https://play.golang.org/p/ZpVEXQbGvoq

Looks like the namespaces might only matter if there’s ambiguity. You probably only need the namespaces if you’re doing something like this: https://play.golang.org/p/0T9sy6Lv7UA

thx