A
A
Alexander Kulaga2020-06-18 21:22:58
go
Alexander Kulaga, 2020-06-18 21:22:58

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

2 answer(s)
S
ScriptKiddo, 2020-06-18
@ScriptKiddo

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

OUT
Status: OK
Process finished with exit code 0

I
Ivan Koryukov, 2020-06-19
@MadridianFox

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 question

Ask a Question

731 491 924 answers to any question