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)
}