duandi8852752 2017-10-05 18:27
浏览 28
已采纳

在Go中解封SOAP响应

I am making a SOAP call to an API, Here is a sample response:

<?xml version="1.0" encoding="utf-8" ?>
 <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:body>
      <soapenv:fault>
        <faultcode>
          ERR109
        </faultcode>
        <faultstring>
          Account Expired. Result code is 2163
        </faultstring>
        <detail>
          <ns1:serviceexception xmlns:ns1="http://www.csapi.org/schema/parlayx/common/v2_1">
            <messageid>
              ERR109
            </messageid>
            <text>
              Account Expired. Result code is 2163
            </text>
            <variables>
              2163
            </variables>
          </ns1:serviceexception>
        </detail>
      </soapenv:fault>
    </soapenv:body>
  </soapenv:envelope>

To unmarshal this response, I have built some structs:

type SoapResponse struct {
    Body    ResponseBody `soapenv:"body"`
}
type ResponseBody struct {
    Fault   Fault    `soapenv:"fault"`
}
type Fault struct {
    FaultCode   string `xml:"faultcode"`
    FaultString string `xml:"faultstring"`
    Detail      Detail `xml:"detail"`
}
type Detail struct {
    ServiceException ServiceException `ns1:"serviceexception"`
}
type ServiceException struct {
    ID          string `xml:"messageid"` 
    MessageText string `xml:"text"`
    ErrorCode   string `xml:"variables"`
}

And here is the code that does the unmarshaling part:

responseBody, _:= ioutil.ReadAll(resp.Body)
var soapResponse = new(SoapResponse)
err := xml.Unmarshal(responseBody, soapResponse)
    if err != nil {
        panic("Error!")
    }

The problem is that all soapResponse properties are populated just right, except for soapResponse.Body.Fault.Detail.ServiceException.ID which prints nothing.
I couldn't figure out why. Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • dtn51137 2017-10-05 20:10
    关注

    You may parse got XML with such structures:

    type SoapResponse struct {
        Body ResponseBody `xml:"soapenv body"`
    }
    type ResponseBody struct {
        Fault Fault `xml:"fault"`
    }
    type Fault struct {
        FaultCode   string `xml:"faultcode"`
        FaultString string `xml:"faultstring"`
        Detail      Detail `xml:"detail"`
    }
    type Detail struct {
        ServiceException ServiceException `xml:"serviceexception"`
    }
    type ServiceException struct {
        ID          string `xml:"messageid"`
        MessageText string `xml:"text"`
        ErrorCode   string `xml:"variables"`
    }
    

    I've added namespace for the first element and fixed some definitions. Working example - https://play.golang.org/p/vZQhaxYikX

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?