首頁 > 後端開發 > Golang > 主體

使用 Go 解組 SOAP 訊息

WBOY
發布: 2024-02-06 09:21:14
轉載
1185 人瀏覽過

使用 Go 解组 SOAP 消息

問題內容

我對 go 語言還比較陌生。

我在嘗試解組 soap 訊息時遇到問題。我的嘗試是抽象化 body 元素的內容並避免靜態定義 xml 結構,因為它會根據請求的操作而變化。 不幸的是我找不到正確的方法。在範例中,getcontent 函數應接收指向包含內容的結構的指針,並將其動態新增至 body 中,以便進行填充。但結果並不是預期的。

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)
}
登入後複製

這是操場上的範例:

https://go.dev/play/p/bbr4vexipbc


正確答案


我找到的解決方案是使用泛型來表示主體的變量和參數化內容。

此程式碼按我的預期工作:

package main

import (
    "encoding/xml"
    "fmt"
)

type EnvelopeResponse[T any] struct {
    XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
    Body    Body[T]  `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}

type Body[T any] struct {
    XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`

    Fault               *Fault `xml:",omitempty"`
    Content             T      `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[T any](rawXml []byte, content T) {
    envelope := EnvelopeResponse[T]{Body: Body[T]{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)
}
登入後複製

以上是使用 Go 解組 SOAP 訊息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!