这是我的 XML 请求正文:<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ads="http://www.lunalovegood.com/sudarsan/"> <soap:Header/> <soap:Body> <ads:Candy> <ads:toffee>a3f27f3a0a684700e9ab834df492505d806b1944</ads:toffee> </ads:Candy> </soap:Body></soap:Envelope>这是我的结构定义:type CandyMan struct { SOAPEnvelope Body struct { Candy `xml:"Candy"` } `xml:"Body"`}type Candy struct { Toffee string `xml:"toffee"`}type SOAPEnvelope struct { XMLName xml.Name `xml:"Envelope"` Text string `xml:",chardata"` XSI string `xml:"xmlns:xsi,attr"` XSD string `xml:"xmlns:xsd,attr"` Soap string `xml:"xmlns:soap,attr"`}我正在尝试将 XML 解组到结构中:req := dtos.CandyMan{}err := xml.NewDecoder(r.Body).Decode(&req)fmt.Println(req, err)我没有收到任何错误,但解组不起作用。我得到一个空结构,请帮我解决问题。谢谢。
1 回答

繁星coding
TA贡献1797条经验 获得超4个赞
您已将结构嵌入Candy到Body结构中。
你可能不想这样做,而是有这样的事情:
type CandyMan struct {
SOAPEnvelope
Body struct {
Candy Candy `xml:"Candy"`
} `xml:"Body"`
}
它似乎工作正常: https: //play.golang.org/p/ilFsLriOAuP
- 1 回答
- 0 关注
- 73 浏览
添加回答
举报
0/150
提交
取消