title: Unmarshalling a specific SOAP response in go
body: <p>I am trying to unmarshal the following SOAP response using the below structs.   </p>
  
  <pre><code>var data = `&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3rg/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;soap:Body&gt;
          &lt;doSendResponse&gt;
              &lt;doSendResult&gt;Send OK.&amp;lt;ReturnIDs&amp;gt;c71cf425f5;e5e4dbb5ca&amp;lt;/ReturnIDs&amp;gt;&lt;/doSendResult&gt;
          &lt;/doSendResponse&gt;
      &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;`
  
  type ResponseBody struct {
      ResponseBody SendResponse `xml:"Body"`
  }
  type SendResponse struct {
      Result Result `xml:"doSendResponse"`
  }
  type Result struct {
      RawMessage string `xml:"doSendResult"`
  }
  </code></pre>
  
  <p>All goes well until after the <code>&lt;doSendResult&gt;</code> element.<br>
  This particular tag contains a message i.e. "Send OK." and an HTML encoded <code>&lt;ReturnIDs&gt;</code> element, the problem isn't about the HTML encoded parts, I've already seen <a href="https://stackoverflow.com/questions/23585914/unmarshalling-escaped-xml">this question and the accepted answer.</a>
  My problem is that I can't manage to extract both the message and the return IDs.</p>
  
  <p>I tried to use the approach suggested in the previously mentioned question but I failed, <a href="https://play.golang.org/p/iY4LvUArM3" rel="nofollow noreferrer">Here</a> is what I tried so far.</p>
  
  <pre><code>package main
  
  import (
      "encoding/xml"
      "fmt"
  )
  
  var data = `&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3rg/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;soap:Body&gt;
          &lt;doSendResponse&gt;
              &lt;doSendResult&gt;Send OK.&amp;lt;ReturnIDs&amp;gt;c71cf425f5;e5e4dbb5ca&amp;lt;/ReturnIDs&amp;gt;&lt;/doSendResult&gt;
          &lt;/doSendResponse&gt;
      &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;`
  
  type ResponseBody struct {
      ResponseBody SendResponse `xml:"Body"`
  }
  type SendResponse struct {
      Result Result `xml:"doSendResponse"`
  }
  type Result struct {
      RawMessage string `xml:"doSendResult"`
  }
  type RawMessage struct {
      IDs     string `xml:"ReturnIDs"`
  }
  
  func main() {
      var response ResponseBody
      err := xml.Unmarshal([]byte(data), &amp;response)
      if err != nil {
          panic(err.Error())
      }
      fmt.Printf("%+v\n", response)
  
      var rawMessage RawMessage
      err = xml.Unmarshal([]byte(response.ResponseBody.Result.RawMessage), &amp;rawMessage)
      if err != nil {
          panic(err.Error())
      }
      fmt.Printf("%+v\n", rawMessage)
  
  }
  </code></pre>
  
  <p>Output:<br>
  <code>{ResponseBody:{Result:{RawMessage:Send OK.&lt;ReturnIDs&gt;c71cf425f5;e5e4dbb5ca&lt;/ReturnIDs&gt;}}}
  {IDs:}
  </code>
  I also tried to Unesacpe the response, then tried to unmarshal it, it partially works, but there are 3 main problems with this approach:</p>
  
  <ol>
  <li>It's too slow</li>
  <li>I could only either <a href="https://play.golang.org/p/rG7_3uSnTA" rel="nofollow noreferrer">get the ReturnIDs</a> or <a href="https://play.golang.org/p/t2b2xfF704" rel="nofollow noreferrer">the message</a>, not both.</li>
  <li>I believe it's just an ugly hack and there must be a better way to do that (That I'm not aware of, yet.)</li>
  </ol>
  
  <p>So, How can I extract both values for the message (Send OK.) and the <code>&lt;ReturnIDs&gt;</code>?</p>