在Golang中使用命名空间消费SOAP

huangapple go评论116阅读模式
英文:

Consuming SOAP with namespaces in GOLANG

问题

我是一个GO的新手,开始学习如何处理SOAP请求。我在这里遇到了一个困难,就是不知道如何构建结构体来反映从webservice中传入的这种数据,以便进行解组。你能给我一些建议吗?我正在使用GO 1.5.1版本。

以下是要翻译的内容:

我是一个GO的新手,开始学习如何处理SOAP请求。我在这里遇到了一个困难,就是不知道如何构建结构体来反映从webservice中传入的这种数据,以便进行解组。你能给我一些建议吗?我正在使用GO 1.5.1版本。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://webapi.allegro.pl/service.php">
  3. <SOAP-ENV:Body>
  4. <ns1:doQueryAllSysStatusResponse>
  5. <ns1:sysCountryStatus>
  6. <ns1:item>
  7. <ns1:countryId>1</ns1:countryId>
  8. <ns1:programVersion>1.0</ns1:programVersion>
  9. <ns1:catsVersion>1.1.87</ns1:catsVersion>
  10. <ns1:apiVersion>1.0</ns1:apiVersion>
  11. <ns1:attribVersion>1.0</ns1:attribVersion>
  12. <ns1:formSellVersion>1.4.46</ns1:formSellVersion>
  13. <ns1:siteVersion>1.0</ns1:siteVersion>
  14. <ns1:verKey>123131231</ns1:verKey>
  15. </ns1:item>
  16. <ns1:item>
  17. <ns1:countryId>56</ns1:countryId>
  18. <ns1:programVersion>1.0</ns1:programVersion>
  19. <ns1:catsVersion>1.0.43</ns1:catsVersion>
  20. <ns1:apiVersion>1.0</ns1:apiVersion>
  21. <ns1:attribVersion>1.0</ns1:attribVersion>
  22. <ns1:formSellVersion>1.0.69</ns1:formSellVersion>
  23. <ns1:siteVersion>1.0</ns1:siteVersion>
  24. <ns1:verKey>00000101</ns1:verKey>
  25. </ns1:item>
  26. </ns1:sysCountryStatus>
  27. </ns1:doQueryAllSysStatusResponse>
  28. </SOAP-ENV:Body>
  29. </SOAP-ENV:Envelope>
英文:

i'm a GO newbie, starting to learn how to process SOAP requests. I have a difficulty here with the namespaces: i don't know how to contruct the structs to reflect such kind of data coming in from the webservice in order to unmarshall it. Could you give me a few hints here? I'm using GO 1.5.1

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:ns1=&quot;https://webapi.allegro.pl/service.php&quot;&gt;
  3. &lt;SOAP-ENV:Body&gt;
  4. &lt;ns1:doQueryAllSysStatusResponse&gt;
  5. &lt;ns1:sysCountryStatus&gt;
  6. &lt;ns1:item&gt;
  7. &lt;ns1:countryId&gt;1&lt;/ns1:countryId&gt;
  8. &lt;ns1:programVersion&gt;1.0&lt;/ns1:programVersion&gt;
  9. &lt;ns1:catsVersion&gt;1.1.87&lt;/ns1:catsVersion&gt;
  10. &lt;ns1:apiVersion&gt;1.0&lt;/ns1:apiVersion&gt;
  11. &lt;ns1:attribVersion&gt;1.0&lt;/ns1:attribVersion&gt;
  12. &lt;ns1:formSellVersion&gt;1.4.46&lt;/ns1:formSellVersion&gt;
  13. &lt;ns1:siteVersion&gt;1.0&lt;/ns1:siteVersion&gt;
  14. &lt;ns1:verKey&gt;123131231&lt;/ns1:verKey&gt;
  15. &lt;/ns1:item&gt;
  16. &lt;ns1:item&gt;
  17. &lt;ns1:countryId&gt;56&lt;/ns1:countryId&gt;
  18. &lt;ns1:programVersion&gt;1.0&lt;/ns1:programVersion&gt;
  19. &lt;ns1:catsVersion&gt;1.0.43&lt;/ns1:catsVersion&gt;
  20. &lt;ns1:apiVersion&gt;1.0&lt;/ns1:apiVersion&gt;
  21. &lt;ns1:attribVersion&gt;1.0&lt;/ns1:attribVersion&gt;
  22. &lt;ns1:formSellVersion&gt;1.0.69&lt;/ns1:formSellVersion&gt;
  23. &lt;ns1:siteVersion&gt;1.0&lt;/ns1:siteVersion&gt;
  24. &lt;ns1:verKey&gt;00000101&lt;/ns1:verKey&gt;
  25. &lt;/ns1:item&gt;
  26. &lt;/ns1:sysCountryStatus&gt;
  27. &lt;/ns1:doQueryAllSysStatusResponse&gt;
  28. &lt;/SOAP-ENV:Body&gt;
  29. &lt;/SOAP-ENV:Envelope&gt;

答案1

得分: 1

你可以使用与你的SOAP数据匹配的结构体,并使用"encoding/xml"包进行解组。

结构体:

  1. type Envelope struct {
  2. XMLName xml.Name `xml:"SOAP-ENV:Envelope"`
  3. Body Body `xml:"SOAP-ENV:Body"`
  4. }
  5. type Body struct {
  6. StatusRes *DoQueryAllSysStatusResponse `xml:"ns1:doQueryAllSysStatusResponse"`
  7. }
  8. type DoQueryAllSysStatusResponse struct {
  9. CountryStatus *SysCountryStatus `xml:"ns1:sysCountryStatus"`
  10. }
  11. // ...

解组:

  1. data := []byte{} // SOAP数据
  2. env := &Envelope{}
  3. err := xml.Unmarshal(data, env)
  4. if err != nil {
  5. // 处理错误
  6. }

以上是给定代码的翻译结果。

英文:

You can a structure that matches your SOAP data and then unmarshal it with the "encoding/xml" package

Structs :

  1. type Envelope struct {
  2. XMLName xml.Name `xml:&quot;SOAP-ENV:Envelope&quot;`
  3. Body Body `xml:&quot;SOAP-ENV:Body&quot;`
  4. }
  5. type Body struct {
  6. StatusRes *DoQueryAllSysStatusResponse `xml:&quot;ns1:doQueryAllSysStatusResponse&quot;`
  7. }
  8. type DoQueryAllSysStatusResponse struct {
  9. CountryStatus *SysCountryStatus `xml:&quot;ns1:sysCountryStatus&quot;`
  10. }
  11. // ...

Unmarshal:

  1. data := []byte{} // SOAP data
  2. env := &amp;Envelope{}
  3. err := xml.Unmarshal(data, env)
  4. if err != nil {
  5. // do something
  6. }

huangapple
  • 本文由 发表于 2015年10月5日 18:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/32946927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定