2015-10-05 21 views
0

我是GO的新手,開始學習如何處理SOAP請求。我在命名空間方面遇到了一些困難:我不知道如何構建結構體來反映web服務中的這類數據,以解開它。你能在這裏給我幾點提示嗎?我使用GO 1.5.1在GOLANG中使用名稱空間的SOAP

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://webapi.allegro.pl/service.php"> 
<SOAP-ENV:Body> 
    <ns1:doQueryAllSysStatusResponse> 
     <ns1:sysCountryStatus> 

      <ns1:item> 
       <ns1:countryId>1</ns1:countryId> 
       <ns1:programVersion>1.0</ns1:programVersion> 
       <ns1:catsVersion>1.1.87</ns1:catsVersion> 
       <ns1:apiVersion>1.0</ns1:apiVersion> 
       <ns1:attribVersion>1.0</ns1:attribVersion> 
       <ns1:formSellVersion>1.4.46</ns1:formSellVersion> 
       <ns1:siteVersion>1.0</ns1:siteVersion> 
       <ns1:verKey>123131231</ns1:verKey> 
      </ns1:item> 

      <ns1:item> 
       <ns1:countryId>56</ns1:countryId> 
       <ns1:programVersion>1.0</ns1:programVersion> 
       <ns1:catsVersion>1.0.43</ns1:catsVersion> 
       <ns1:apiVersion>1.0</ns1:apiVersion> 
       <ns1:attribVersion>1.0</ns1:attribVersion> 
       <ns1:formSellVersion>1.0.69</ns1:formSellVersion> 
       <ns1:siteVersion>1.0</ns1:siteVersion> 
       <ns1:verKey>00000101</ns1:verKey> 
      </ns1:item> 
     </ns1:sysCountryStatus> 
    </ns1:doQueryAllSysStatusResponse> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 
+0

'encoding/xml'標準包本身支持命名空間 - 編碼和解碼規則在這裏解釋[https:// golan g.org/pkg/encoding/xml/#Marshal)和[there](https://golang.org/pkg/encoding/xml/#Unmarshal)。參考手冊有點幹,所以你嘗試[搜索](http://stackoverflow.com/search?q= [去] + XML +命名空間)?特別是,[this](http://stackoverflow.com/q/10812294/720999)可能是這種方法的很好例證。 – kostix

+0

您好kostix和謝謝你的偉大的建議。我認爲你提供的鏈接(http://stackoverflow.com/q/10812294/720999)回答我所有的dilemas,好像我錯過了「ATTR」 – Greg

回答

1

您可以將您的SOAP數據,並與 「編碼/ XML」 包

的Structs匹配,那麼解組它的結構:

type Envelope struct { 
    XMLName xml.Name `xml:"SOAP-ENV:Envelope"` 
    Body  Body  `xml:"SOAP-ENV:Body"` 
} 

type Body struct { 
    StatusRes *DoQueryAllSysStatusResponse `xml:"ns1:doQueryAllSysStatusResponse"` 
} 

type DoQueryAllSysStatusResponse struct { 
    CountryStatus *SysCountryStatus `xml:"ns1:sysCountryStatus"` 
} 

// ... 

解組:

data := []byte{} // SOAP data 
env := &Envelope{} 
err := xml.Unmarshal(data, env) 
if err != nil { 
    // do something 
} 
+0

喜basgys的功能,這是我最初試圖接近但它產生了以下消息: 預期的元素類型但具有 這就是爲什麼我開始diggind進一步,無論如何,謝謝! – Greg