gpt4 book ai didi

xml - 在 Go 中解析 SOAP

转载 作者:IT王子 更新时间:2023-10-29 00:57:58 24 4
gpt4 key购买 nike

我刚开始学习围棋。我想解析一个 SOAP 服务。我很难解析 XML。这是 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>

下面是我为解析 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)
}

为什么返回nil .我希望从中获取用户名值。还有为什么我不能使用 xml:"SOAP-ENV:Envelope"对于 Envelop 结构上的 XMLName?错误信息是expected element type <SOAP-ENV:Envelope> but have <Envelope>

我的 Go 版本是 1.8.3

最佳答案

只需使用 xml:"UsernameToken"而不是 xml:"wsse:UsernameToken" , xml:"wsse:Security" -> xml:"Security"

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)
}

输出:USERNAME <nil>

关于xml - 在 Go 中解析 SOAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45456344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com