我对围棋这门语言还比较陌生.
我在try 解组一条SOAP消息时遇到了问题.我的try 是抽象Body元素的内容,避免静态地定义XML struct ,因为它会根据请求的操作而变化. 不幸的是,我找不到一种方法来正确地做到这一点.在该示例中,GetContent函数应接收指向包含内容的 struct 的指针,并将其动态添加到正文中,以便进行填充.但结果并不是预期的那样.
package main
import (
"encoding/xml"
"fmt"
)
type Message interface{}
type EnvelopeResponse struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
Body Message `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}
type Body struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
Fault *Fault `xml:",omitempty"`
Content Message `xml:",omitempty"`
SOAPBodyContentType string `xml:"-"`
}
type Fault struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault"`
Code string `xml:"faultcode,omitempty"`
String string `xml:"faultstring,omitempty"`
Actor string `xml:"faultactor,omitempty"`
Detail string `xml:"detail,omitempty"`
}
type GetHostNumberOfEntriesResponse struct {
XMLName xml.Name `xml:"urn:dslforum-org:service:Hosts:1 GetHostNumberOfEntriesResponse"`
NewHostNumberOfEntries int64 `xml:"NewHostNumberOfEntries"`
}
func GetContent(rawXml []byte, content interface{}) {
envelope := EnvelopeResponse{Body: Body{Content: content}}
xml.Unmarshal(rawXml, &envelope)
}
func main() {
b := []byte(`
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetHostNumberOfEntriesResponse xmlns:u="urn:dslforum-org:service:Hosts:1">
<NewHostNumberOfEntries>47</NewHostNumberOfEntries>
</u:GetHostNumberOfEntriesResponse>
</s:Body>
</s:Envelope>
`)
content := &GetHostNumberOfEntriesResponse{}
GetContent(b, content)
fmt.Println(*content)
}
下面是操场上的例子: