将XML解组到结构中时出现空结果

  • 本文关键字:结果 结构 XML xml go soap
  • 更新时间 :
  • 英文 :


这是我的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)

我没有犯任何错误,但整理不起作用。我得到了一个空结构,请帮我解决这个问题。谢谢

您已经将Candy结构嵌入到Body结构中。

你可能不想那样做,而是有这样的东西:

type CandyMan struct {
SOAPEnvelope
Body struct {
Candy Candy `xml:"Candy"`
} `xml:"Body"`
}

当时似乎运行良好:https://play.golang.org/p/ilFsLriOAuP

最新更新