【发布时间】:2019-03-23 12:48:03
【问题描述】:
我正在尝试将 Go 结构编码为 Soap-Envelope (xml)。 到目前为止,除了一个小错误,肥皂体看起来还不错。 虽然我的信封应该是这样的:
<SOAP-ENV:Body>
<q3:WMLS_AddToStore xmlns:q3="http://www.foo.abr/message/120">
<WMLtypeIn>param1</WMLtypeIn>
<XMLin>param2</XMLin>
<OptionsIn>param3</OptionsIn>
<CapabilitiesIn>param4</CapabilitiesIn>
</q3:WMLS_AddToStore>
</SOAP-ENV:Body>
我的代码正在生成这个:
<SOAP-ENV:Body>
<q1:WMLS_AddToStore xmlns:q1="http://www.foo.abr/message/120">
<ActionName>
<WMLtypeIn>param1</WMLtypeIn>
<XMLin>param2</XMLin>
<OptionsIn>param3</OptionsIn>
<CapabilitiesIn>param4</CapabilitiesIn>
</ActionName>
</q1:WMLS_AddToStore>
</SOAP-ENV:Body>
注意 ActionName 标签。我想要:
删除此 ActionName 标记
或
将其重命名为q3:WMLS_AddToStore(我可以这样做),但随后我需要为其添加xmlns:q1 属性。
代码:
type Message interface{}
type OperationWMLS_AddToStoreSoapIn struct {
WMLtypeIn string `xml:"WMLtypeIn,omitempty"`
XMLin string `xml:"XMLin,omitempty"`
OptionsIn string `xml:"OptionsIn,omitempty"`
CapabilitiesIn string `xml:"CapabilitiesIn,omitempty"`
}
type Body struct {
XMLName xml.Name `xml:"SOAP-ENV:Body"`
ActionName temperature `xml:"q1:WMLS_AddToStore"`
}
type Action struct {
ActionName Message `xml:",innerxml"`
XMLAttr string `xml:"xmlns:q1,attr"`
}
func main() {
in := struct {
OperationWMLS_AddToStoreSoapIn `xml:"tns:WMLS_AddToStore"`
}{
OperationWMLS_AddToStoreSoapIn{
"WMLtypeIn",
"XMLin",
"OptionsIn",
"CapabilitiesIn",
},
}
x := &Body{
ActionName: Action{
ActionName: in,
XMLAttr: "http://www.foo.abr/message/120",
},
}
enc := xml.NewEncoder(os.Stdout)
enc.Indent("", " ")
if err := enc.Encode(x); err != nil {
fmt.Printf("error: %v\n", err)
}
}
【问题讨论】:
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码.
-
阿德里安,操场代码正在运行。我只需要摆脱 ActionName 标记。我将在问题中添加代码。