Answer the question
In order to leave comments, you need to log in
How to deserialize SOAP?
I'm trying to deploy SOAP to a GO structure, it doesn't return an error, but the status field remains empty. What am I doing wrong?
package main
import (
"fmt"
"encoding/xml"
)
type myEnvelope struct {
XMLName xml.Name `xml:"Envelope"`
Body methodResponseData
}
type methodResponseData struct {
XMLName xml.Name
Status string `xml:"responseData>status"`
}
func main() {
resp := methodResponseData{}
resp.XMLName.Local = "SOAP-ENV:methodResponse"
envelope := myEnvelope{
Body: resp,
}
envelope.XMLName.Local = "Envelope"
envelope.Body.XMLName.Local = "Body"
raw := []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<SOAP-ENV:methodResponse>
<responseData>
<status>OK</status>
</responseData>
</SOAP-ENV:methodResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
`)
err := xml.Unmarshal(raw, &envelope)
if err != nil {
fmt.Printf("something wrong %s", err)
}
fmt.Printf("%s", envelope.Body.Status)
}
Answer the question
In order to leave comments, you need to log in
I don't know much about Go, so this might look weird.
package main
import (
"fmt"
"encoding/xml"
)
type Body struct {
XMLName xml.Name
Status string `xml:"methodResponse>responseData>status"`
}
type Envelope struct {
XMLName xml.Name
Body Body `xml:"Body"`
}
func main() {
var envelope Envelope
raw := []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<SOAP-ENV:methodResponse>
<responseData>
<status>OK</status>
</responseData>
</SOAP-ENV:methodResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
`)
err := xml.Unmarshal(raw, &envelope)
if err != nil {
fmt.Printf("something wrong %s", err)
}
fmt.Printf("Status: %s", envelope.Body.Status)
}
Status: OK
Process finished with exit code 0
Don't reinvent the wheel and find a library to work with soap. Protocols are invented for this, in order to standardize some things and then use libraries.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question