在Go中解析SOAP

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

Parse SOAP in Go

问题

我刚开始学习Go语言。我想解析一个SOAP服务。我在解析XML时遇到了困难。以下是XML的内容:

  1. <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  2. <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  3. <wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  4. <wsse:UsernameToken>
  5. <wsse:Username>USERNAME</wsse:Username>
  6. <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SECRET</wsse:Password>
  7. </wsse:UsernameToken>
  8. </wsse:Security>
  9. </SOAP-ENV:Header>
  10. <SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  11. <OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="abc123" Version="1.0" TimeStamp="2005-08-01T09:30:47+08:00">
  12. <AvailStatusMessages HotelCode="HOTEL">
  13. <AvailStatusMessage BookingLimit="10">
  14. <StatusApplicationControl Start="2010-01-01" End="2010-01-14" InvTypeCode="A1K" RatePlanCode="GLD"/>
  15. </AvailStatusMessage>
  16. </AvailStatusMessages>
  17. </OTA_HotelAvailNotifRQ>
  18. </SOAP-ENV:Body>
  19. </SOAP-ENV:Envelope>

以下是我编写的用于解析XML的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "encoding/xml"
  5. )
  6. type Envelope struct {
  7. XMLName xml.Name
  8. SOAPENV string `xml:"xmlns:SOAP-ENV,attr"`
  9. XSD string `xml:"xmlns:xsd,attr"`
  10. XSI string `xml:"xmlns:xsi,attr"`
  11. SOAPENC string `xml:"xmlns:SOAP-ENC,attr"`
  12. NS9132 string `xml:"xmlns:ns9132,attr"`
  13. Header Header `xml:"SOAP-ENV:Header"`
  14. }
  15. type Header struct {
  16. XMLName xml.Name `xml:"SOAP-ENV:Header"`
  17. Security Security `xml:"wsse:Security"`
  18. }
  19. type Security struct {
  20. XMLName xml.Name `xml:"wsse:Security"`
  21. MustUnderstand string `xml:"soap:mustUnderstand,attr"`
  22. WSSE string `xml:"xmlns:wsse,attr"`
  23. SOAP string `xml:"xmlns:soap,attr"`
  24. UsernameToken struct {
  25. XMLName xml.Name `xml:"wsse:UsernameToken"`
  26. Username string `xml:"wsse:Username"`
  27. Password string `xml:"wsse:Password"`
  28. }
  29. }
  30. func main() {
  31. Soap := []byte(`<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  32. <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  33. <wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  34. <wsse:UsernameToken>
  35. <wsse:Username>USERNAME</wsse:Username>
  36. <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SECRET</wsse:Password>
  37. </wsse:UsernameToken>
  38. </wsse:Security>
  39. </SOAP-ENV:Header>
  40. <SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  41. <OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="abc123" Version="1.0" TimeStamp="2005-08-01T09:30:47+08:00">
  42. <AvailStatusMessages HotelCode="HOTEL">
  43. <AvailStatusMessage BookingLimit="10">
  44. <StatusApplicationControl Start="2010-01-01" End="2010-01-14" InvTypeCode="A1K" RatePlanCode="GLD"/>
  45. </AvailStatusMessage>
  46. </AvailStatusMessages>
  47. </OTA_HotelAvailNotifRQ>
  48. </SOAP-ENV:Body>
  49. </SOAP-ENV:Envelope>`)
  50. res := &Envelope{}
  51. err := xml.Unmarshal(Soap, res)
  52. fmt.Println(res.Header.Security.UsernameToken.Username, err)
  53. }

为什么返回nil?我期望从中获取用户名。另外,为什么我不能在Envelope结构体上使用xml:"SOAP-ENV:Envelope"作为XMLName?错误消息是expected element type <SOAP-ENV:Envelope> but have <Envelope>

我的Go版本是1.8.3。

英文:

I just started learning Go. I want to parse a SOAP service. I have difficulty parsing the XML. Here's the XML:

  1. &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  2. &lt;SOAP-ENV:Header xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  3. &lt;wsse:Security soap:mustUnderstand=&quot;1&quot; xmlns:wsse=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot; xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  4. &lt;wsse:UsernameToken&gt;
  5. &lt;wsse:Username&gt;USERNAME&lt;/wsse:Username&gt;
  6. &lt;wsse:Password Type=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText&quot;&gt;SECRET&lt;/wsse:Password&gt;
  7. &lt;/wsse:UsernameToken&gt;
  8. &lt;/wsse:Security&gt;
  9. &lt;/SOAP-ENV:Header&gt;
  10. &lt;SOAP-ENV:Body xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  11. &lt;OTA_HotelAvailNotifRQ xmlns=&quot;http://www.opentravel.org/OTA/2003/05&quot; EchoToken=&quot;abc123&quot; Version=&quot;1.0&quot; TimeStamp=&quot;2005-08-01T09:30:47+08:00&quot;&gt;
  12. &lt;AvailStatusMessages HotelCode=&quot;HOTEL&quot;&gt;
  13. &lt;AvailStatusMessage BookingLimit=&quot;10&quot;&gt;
  14. &lt;StatusApplicationControl Start=&quot;2010-01-01&quot; End=&quot;2010-01-14&quot; InvTypeCode=&quot;A1K&quot; RatePlanCode=&quot;GLD&quot;/&gt;
  15. &lt;/AvailStatusMessage&gt;
  16. &lt;/AvailStatusMessages&gt;
  17. &lt;/OTA_HotelAvailNotifRQ&gt;
  18. &lt;/SOAP-ENV:Body&gt;
  19. &lt;/SOAP-ENV:Envelope&gt;

And here's the code I write to parse the XML:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;encoding/xml&quot;
  5. )
  6. type Envelope struct {
  7. XMLName xml.Name
  8. SOAPENV string `xml:&quot;xmlns:SOAP-ENV,attr&quot;`
  9. XSD string `xml:&quot;xmlns:xsd,attr&quot;`
  10. XSI string `xml:&quot;xmlns:xsi,attr&quot;`
  11. SOAPENC string `xml:&quot;xmlns:SOAP-ENC,attr&quot;`
  12. NS9132 string `xml:&quot;xmlns:ns9132,attr&quot;`
  13. Header Header `xml:&quot;SOAP-ENV:Header&quot;`
  14. }
  15. type Header struct {
  16. XMLName xml.Name `xml:&quot;SOAP-ENV:Header&quot;`
  17. Security Security `xml:&quot;wsse:Security&quot;`
  18. }
  19. type Security struct {
  20. XMLName xml.Name `xml:&quot;wsse:Security&quot;`
  21. MustUnderstand string `xml:&quot;soap:mustUnderstand,attr&quot;`
  22. WSSE string `xml:&quot;xmlns:wsse,attr&quot;`
  23. SOAP string `xml:&quot;xmlns:soap,attr&quot;`
  24. UsernameToken struct {
  25. XMLName xml.Name `xml:&quot;wsse:UsernameToken&quot;`
  26. Username string `xml:&quot;wsse:Username&quot;`
  27. Password string `xml:&quot;wsse:Password&quot;`
  28. }
  29. }
  30. func main() {
  31. Soap := []byte(`&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  32. &lt;SOAP-ENV:Header xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  33. &lt;wsse:Security soap:mustUnderstand=&quot;1&quot; xmlns:wsse=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot; xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  34. &lt;wsse:UsernameToken&gt;
  35. &lt;wsse:Username&gt;USERNAME&lt;/wsse:Username&gt;
  36. &lt;wsse:Password Type=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText&quot;&gt;SECRET&lt;/wsse:Password&gt;
  37. &lt;/wsse:UsernameToken&gt;
  38. &lt;/wsse:Security&gt;
  39. &lt;/SOAP-ENV:Header&gt;
  40. &lt;SOAP-ENV:Body xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  41. &lt;OTA_HotelAvailNotifRQ xmlns=&quot;http://www.opentravel.org/OTA/2003/05&quot; EchoToken=&quot;abc123&quot; Version=&quot;1.0&quot; TimeStamp=&quot;2005-08-01T09:30:47+08:00&quot;&gt;
  42. &lt;AvailStatusMessages HotelCode=&quot;HOTEL&quot;&gt;
  43. &lt;AvailStatusMessage BookingLimit=&quot;10&quot;&gt;
  44. &lt;StatusApplicationControl Start=&quot;2010-01-01&quot; End=&quot;2010-01-14&quot; InvTypeCode=&quot;A1K&quot; RatePlanCode=&quot;GLD&quot;/&gt;
  45. &lt;/AvailStatusMessage&gt;
  46. &lt;/AvailStatusMessages&gt;
  47. &lt;/OTA_HotelAvailNotifRQ&gt;
  48. &lt;/SOAP-ENV:Body&gt;
  49. &lt;/SOAP-ENV:Envelope&gt;`)
  50. res := &amp;Envelope{}
  51. err := xml.Unmarshal(Soap, res)
  52. fmt.Println(res.Header.Security.UsernameToken.Username, err)
  53. }

Why is it returns nil. I expect to get the username value from it. Also why can't I use xml:&quot;SOAP-ENV:Envelope&quot; for XMLName on Envelop struct? The error message is expected element type &lt;SOAP-ENV:Envelope&gt; but have &lt;Envelope&gt;

My Go version is 1.8.3

答案1

得分: 6

只需将以下部分翻译为中文:

只需将xml:&quot;wsse:UsernameToken&quot;替换为xml:&quot;UsernameToken&quot;,将xml:&quot;wsse:Security&quot;替换为xml:&quot;Security&quot;,等等。

  1. package main
  2. import (
  3. "fmt"
  4. "encoding/xml"
  5. )
  6. type Envelope struct {
  7. XMLName xml.Name
  8. Header Header
  9. }
  10. type Header struct {
  11. XMLName xml.Name `xml:"Header"`
  12. Security Security `xml:"Security"`
  13. }
  14. type Security struct {
  15. XMLName xml.Name `xml:"Security"`
  16. MustUnderstand string `xml:"mustUnderstand,attr"`
  17. WSSE string `xml:"wsse,attr"`
  18. SOAP string `xml:"soap,attr"`
  19. UsernameToken struct {
  20. XMLName xml.Name `xml:"UsernameToken"`
  21. Username string `xml:"Username"`
  22. Password string `xml:"Password"`
  23. }
  24. }
  25. func main() {
  26. Soap := []byte(`<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  27. <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  28. <wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  29. <wsse:UsernameToken>
  30. <wsse:Username>USERNAME</wsse:Username>
  31. <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SECRET</wsse:Password>
  32. </wsse:UsernameToken>
  33. </wsse:Security>
  34. </SOAP-ENV:Header>
  35. <SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  36. <OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="abc123" Version="1.0" TimeStamp="2005-08-01T09:30:47+08:00">
  37. <AvailStatusMessages HotelCode="HOTEL">
  38. <AvailStatusMessage BookingLimit="10">
  39. <StatusApplicationControl Start="2010-01-01" End="2010-01-14" InvTypeCode="A1K" RatePlanCode="GLD"/>
  40. </AvailStatusMessage>
  41. </AvailStatusMessages>
  42. </OTA_HotelAvailNotifRQ>
  43. </SOAP-ENV:Body>
  44. </SOAP-ENV:Envelope>`)
  45. res := &Envelope{}
  46. err := xml.Unmarshal(Soap, res)
  47. fmt.Println(res.Header.Security.UsernameToken.Username, err)
  48. }

输出:USERNAME <nil>

英文:

Just use xml:&quot;UsernameToken&quot; instead of xml:&quot;wsse:UsernameToken&quot;, xml:&quot;wsse:Security&quot; -> xml:&quot;Security&quot;, etc.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;encoding/xml&quot;
  5. )
  6. type Envelope struct {
  7. XMLName xml.Name
  8. Header Header
  9. }
  10. type Header struct {
  11. XMLName xml.Name `xml:&quot;Header&quot;`
  12. Security Security `xml:&quot;Security&quot;`
  13. }
  14. type Security struct {
  15. XMLName xml.Name `xml:&quot;Security&quot;`
  16. MustUnderstand string `xml:&quot;mustUnderstand,attr&quot;`
  17. WSSE string `xml:&quot;wsse,attr&quot;`
  18. SOAP string `xml:&quot;soap,attr&quot;`
  19. UsernameToken struct {
  20. XMLName xml.Name `xml:&quot;UsernameToken&quot;`
  21. Username string `xml:&quot;Username&quot;`
  22. Password string `xml:&quot;Password&quot;`
  23. }
  24. }
  25. func main() {
  26. Soap := []byte(`&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  27. &lt;SOAP-ENV:Header xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  28. &lt;wsse:Security soap:mustUnderstand=&quot;1&quot; xmlns:wsse=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd&quot; xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  29. &lt;wsse:UsernameToken&gt;
  30. &lt;wsse:Username&gt;USERNAME&lt;/wsse:Username&gt;
  31. &lt;wsse:Password Type=&quot;http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText&quot;&gt;SECRET&lt;/wsse:Password&gt;
  32. &lt;/wsse:UsernameToken&gt;
  33. &lt;/wsse:Security&gt;
  34. &lt;/SOAP-ENV:Header&gt;
  35. &lt;SOAP-ENV:Body xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  36. &lt;OTA_HotelAvailNotifRQ xmlns=&quot;http://www.opentravel.org/OTA/2003/05&quot; EchoToken=&quot;abc123&quot; Version=&quot;1.0&quot; TimeStamp=&quot;2005-08-01T09:30:47+08:00&quot;&gt;
  37. &lt;AvailStatusMessages HotelCode=&quot;HOTEL&quot;&gt;
  38. &lt;AvailStatusMessage BookingLimit=&quot;10&quot;&gt;
  39. &lt;StatusApplicationControl Start=&quot;2010-01-01&quot; End=&quot;2010-01-14&quot; InvTypeCode=&quot;A1K&quot; RatePlanCode=&quot;GLD&quot;/&gt;
  40. &lt;/AvailStatusMessage&gt;
  41. &lt;/AvailStatusMessages&gt;
  42. &lt;/OTA_HotelAvailNotifRQ&gt;
  43. &lt;/SOAP-ENV:Body&gt;
  44. &lt;/SOAP-ENV:Envelope&gt;`)
  45. res := &amp;Envelope{}
  46. err := xml.Unmarshal(Soap, res)
  47. fmt.Println(res.Header.Security.UsernameToken.Username, err)
  48. }

Output: USERNAME &lt;nil&gt;

答案2

得分: 0

这似乎是一个XML问题。BayRinat的答案解决了这个问题。通常情况下,你可以尝试使用一个SOAP库:https://golanglibs.com/top?q=soap

https://github.com/mfenniak/go-soap

https://github.com/justwatchcom/goat

https://github.com/fiorix/wsdl2go

英文:

That seems like an XML problem. BayRinat's answer addresses this issue. Generally, you could try a SOAP library: https://golanglibs.com/top?q=soap

https://github.com/mfenniak/go-soap

https://github.com/justwatchcom/goat

https://github.com/fiorix/wsdl2go

huangapple
  • 本文由 发表于 2017年8月2日 17:07:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/45456344.html
匿名

发表评论

匿名网友

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

确定