我正在 Go 系统中实现一项从外部 SOAP 服务读取数据的服务。现在我正在为其编写测试,我遇到了这个问题:
unable to unmarshal request body for testing: expected element type <soapenv:Envelope> but have <Envelope>
如果我转储数据,我会得到以下结果:
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:v2 [...]
所以我在 XML 中有正确的命名空间。根据记录,这是我试图将数据解组到的结构:
type collectRequestXML struct {
XMLName xml.Name `xml:"soapenv:Envelope"`
SoapEnv string `xml:"xmlns:soapenv,attr"`
XMLNs string `xml:"xmlns:v2,attr"`
Header struct{} `xml:"soapenv:Header"`
Body struct {
[...]
}
}
我该怎么做才能使解组失败?
See 这个游乐场项目供参考 https://play.golang.org/p/0ee2RpL2c7P.
XML 包的文档中关于命名空间的描述还不够。解析非常简单。对命名空间有少量支持。它可以解析 XML,但是当你创建 XML 时,它对命名空间的支持不够。
当您删除结构体中的命名空间信息时,您的结构体就可以工作了XMLName
定义:
type myStruct struct {
XMLName xml.Name `xml:"Envelope"`
SoapEnv string `xml:"xmlns:soapenv,attr"`
Header struct{} `xml:"soapenv:Header"`
Body struct {
MyData string `xml:"my-data"`
}
}
https://play.golang.org/p/UppXwx0X0i9 https://play.golang.org/p/UppXwx0X0i9
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)