dqwnxdhb88531 2017-08-02 09:07
浏览 258
已采纳

在Go中解析SOAP

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

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

  <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <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/">
      <wsse:UsernameToken>
        <wsse:Username>USERNAME</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SECRET</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </SOAP-ENV:Header>

  <SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="abc123" Version="1.0" TimeStamp="2005-08-01T09:30:47+08:00">
      <AvailStatusMessages HotelCode="HOTEL">
        <AvailStatusMessage BookingLimit="10">
          <StatusApplicationControl Start="2010-01-01" End="2010-01-14" InvTypeCode="A1K" RatePlanCode="GLD"/>
        </AvailStatusMessage>
      </AvailStatusMessages>
    </OTA_HotelAvailNotifRQ>
  </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

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

package main

import (
    "fmt"
    "encoding/xml"
)

type Envelope struct {
    XMLName xml.Name
    SOAPENV string   `xml:"xmlns:SOAP-ENV,attr"`
    XSD     string   `xml:"xmlns:xsd,attr"`
    XSI     string   `xml:"xmlns:xsi,attr"`
    SOAPENC string   `xml:"xmlns:SOAP-ENC,attr"`
    NS9132  string   `xml:"xmlns:ns9132,attr"`
    Header  Header   `xml:"SOAP-ENV:Header"`
}

type Header struct {
    XMLName  xml.Name `xml:"SOAP-ENV:Header"`
    Security Security `xml:"wsse:Security"`
}

type Security struct {
    XMLName        xml.Name `xml:"wsse:Security"`
    MustUnderstand string `xml:"soap:mustUnderstand,attr"`
    WSSE           string `xml:"xmlns:wsse,attr"`
    SOAP           string `xml:"xmlns:soap,attr"`
    UsernameToken struct {
        XMLName  xml.Name `xml:"wsse:UsernameToken"`
        Username string `xml:"wsse:Username"`
        Password string `xml:"wsse:Password"`
    }
}

func main() {

    Soap := []byte(`<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

  <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <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/">
      <wsse:UsernameToken>
        <wsse:Username>USERNAME</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SECRET</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </SOAP-ENV:Header>

  <SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="abc123" Version="1.0" TimeStamp="2005-08-01T09:30:47+08:00">
      <AvailStatusMessages HotelCode="HOTEL">
        <AvailStatusMessage BookingLimit="10">
          <StatusApplicationControl Start="2010-01-01" End="2010-01-14" InvTypeCode="A1K" RatePlanCode="GLD"/>
        </AvailStatusMessage>
      </AvailStatusMessages>
    </OTA_HotelAvailNotifRQ>
  </SOAP-ENV:Body>

</SOAP-ENV:Envelope>`)

    res := &Envelope{}
    err := xml.Unmarshal(Soap, res)

    fmt.Println(res.Header.Security.UsernameToken.Username, err)
}

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

My Go version is 1.8.3

  • 写回答

2条回答 默认 最新

  • douke1891 2017-08-02 09:28
    关注

    Just use xml:"UsernameToken" instead of xml:"wsse:UsernameToken", xml:"wsse:Security" -> xml:"Security", etc.

    package main
    
    import (
        "fmt"
        "encoding/xml"
    )
    
    type Envelope struct {
        XMLName xml.Name
        Header  Header
    }
    
    type Header struct {
        XMLName  xml.Name `xml:"Header"`
        Security Security `xml:"Security"`
    }
    
    type Security struct {
        XMLName        xml.Name `xml:"Security"`
        MustUnderstand string `xml:"mustUnderstand,attr"`
        WSSE           string `xml:"wsse,attr"`
        SOAP           string `xml:"soap,attr"`
        UsernameToken struct {
            XMLName  xml.Name `xml:"UsernameToken"`
            Username string `xml:"Username"`
            Password string `xml:"Password"`
        }
    }
    
    func main() {
    
        Soap := []byte(`<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    
      <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <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/">
          <wsse:UsernameToken>
            <wsse:Username>USERNAME</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SECRET</wsse:Password>
          </wsse:UsernameToken>
        </wsse:Security>
      </SOAP-ENV:Header>
    
      <SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="abc123" Version="1.0" TimeStamp="2005-08-01T09:30:47+08:00">
          <AvailStatusMessages HotelCode="HOTEL">
            <AvailStatusMessage BookingLimit="10">
              <StatusApplicationControl Start="2010-01-01" End="2010-01-14" InvTypeCode="A1K" RatePlanCode="GLD"/>
            </AvailStatusMessage>
          </AvailStatusMessages>
        </OTA_HotelAvailNotifRQ>
      </SOAP-ENV:Body>
    
    </SOAP-ENV:Envelope>`)
    
        res := &Envelope{}
        err := xml.Unmarshal(Soap, res)
    
        fmt.Println(res.Header.Security.UsernameToken.Username, err)
    }
    

    Output: USERNAME <nil>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助