dsfadsa08911 2014-05-13 05:01
浏览 423
已采纳

如何解析Golang中的Soap Envelope?

I am new to golang and Soap and having trouble in parsing soap msg.

1.I have an Soap message

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<activationPack_completeResponse"http://tempuri.org/">
<activationPack_completeResult xsi:type="xsd:string">Active</activationPack_completeResult>
</activationPack_completeResponse>
</soap:Body>
</soap:Envelope>

Now how should i Unmarshal them in golang what should be my struct declaration for tag Soap Envelope.

I have some structure as below:

type MyRespEnvelope struct {
    XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
    Soap    *Body
}
type Body struct {
    XMLName     xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
    GetResponse *ActivationPack_CompleteResponse
}
type ActivationPack_CompleteResponse struct {
    XMLName xml.Name `xml:"activationPack_completeResponse"`
    Id      string   `xml:"xmlns,attr"`
    MyVar   string   `xml:"activationPack_completeResult"`
} 

But I am getting error as below:

error: expected element <Envelope> in name space http://schemas.xmlsoap.org/soap/envelope/ but have soap*stopped,reason="end-stepping-range",frame={addr="0x0000000000401211",func="main.UnmarshalFirstDitto",args=[{name="data",value="\"\
Notice: Undefined variable: area in /var/www/nusoap/dittotv.php on line 25\
\
Notice: Undefined variable: area in /var/www/nusoap/dittotv.php on line 27\
\
Notice: Undefined variable: area in /var/www/nu\"..."}],file="/media/winshare/Golang/WorkSpace/src/DittoTv/ditto.go",fullname="/media/winshare/Golang/WorkSpace/src/DittoTv/ditto.go",line="60"},thread-id="1",stopped-threads="all",core="0"

So someone please tell me how should i declare my structure so that i am able to parse the soap message.

  • 写回答

1条回答 默认 最新

  • duanci6484 2014-05-13 14:36
    关注
    1. Your XML was malformed, I assume it's a bad copy-paste. I corrected it, line 4: <activationPack_completeResponse"http://tempuri.org/"> -> <activationPack_completeResponse Id="http://tempuri.org/">

    2. Your types were wrong. in MyRespEnvelopeyou call the Body struct Soap. Without defining its xml name you're not going to get anything. An easier fix is to change the name from Soap to Body.

    3. I'm not an expert in XML, but I think you were doing something wrong with namespaces. simplifying your types a little, here is a working example: http://play.golang.org/p/957GWzfdvN

      package main
      
      import "fmt"
      import "encoding/xml"
      
      type MyRespEnvelope struct {
          XMLName xml.Name
          Body    Body
      }
      
      type Body struct {
          XMLName     xml.Name
          GetResponse completeResponse `xml:"activationPack_completeResponse"`
      }
      
      type completeResponse struct {
          XMLName xml.Name `xml:"activationPack_completeResponse"`
          Id      string   `xml:"Id,attr"`
          MyVar   string   `xml:"activationPack_completeResult"`
      }
      
      func main() {
      
          Soap := []byte(`<?xml version="1.0" encoding="UTF-8"?>
      <soap:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
      <soap:Body>
      <activationPack_completeResponse Id="http://tempuri.org/">
      <activationPack_completeResult xsi:type="xsd:string">Active</activationPack_completeResult>
      </activationPack_completeResponse>
      </soap:Body>
      </soap:Envelope>`)
      
          res := &MyRespEnvelope{}
          err := xml.Unmarshal(Soap, res)
      
          fmt.Println(res.Body, err)
      }
      

      Note: In the code I put together, I don't use pointer to structs but the structs themselves. You can use either depending on how you're intending to use it, and your preferences I guess.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)