gpt4 book ai didi

soap - 如何在 Golang 中解析 Soap Envelope?

转载 作者:IT王子 更新时间:2023-10-29 01:25:31 27 4
gpt4 key购买 nike

我是 golang 和 Soap 的新手,在解析 soap msg 时遇到了问题。

1.我有一条 SOAP 消息

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<activationPack_completeResponse"http://tempuri.org/">
<activationPack_completeResult xsi:type="xsd:string">Active</activationPack_completeResult>
</activationPack_completeResponse>
</soap:Body>
</soap:Envelope>

现在我应该如何在 golang 中解码它们 我应该为标签 Soap Envelope 的结构声明是什么。

我的结构如下:

type MyRespEnvelope struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
Soap *Body
}
type Body struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
GetResponse *ActivationPack_CompleteResponse
}
type ActivationPack_CompleteResponse struct {
XMLName xml.Name `xml:"activationPack_completeResponse"`
Id string `xml:"xmlns,attr"`
MyVar string `xml:"activationPack_completeResult"`
}

但是我得到如下错误:

error: expected element <Envelope> in name space http://schemas.xmlsoap.org/soap/envelope/ but have soap*stopped,reason="end-stepping-range",frame={addr="0x0000000000401211",func="main.UnmarshalFirstDitto",args=[{name="data",value="\"\\nNotice: Undefined variable: area in /var/www/nusoap/dittotv.php on line 25\\n\\nNotice: Undefined variable: area in /var/www/nusoap/dittotv.php on line 27\\n\\nNotice: Undefined variable: area in /var/www/nu\"..."}],file="/media/winshare/Golang/WorkSpace/src/DittoTv/ditto.go",fullname="/media/winshare/Golang/WorkSpace/src/DittoTv/ditto.go",line="60"},thread-id="1",stopped-threads="all",core="0"

所以有人请告诉我应该如何声明我的结构以便我能够解析 soap 消息。

最佳答案

  1. 您的 XML 格式不正确,我认为这是错误的复制粘贴。我更正了它,第 4 行:<activationPack_completeResponse"http://tempuri.org/"> -> <activationPack_completeResponse Id="http://tempuri.org/">

  2. 您的类型有误。在 MyRespEnvelope你调用Body结构 Soap .如果不定义其 xml 名称,您将一无所获。更简单的解决方法是将名称从 Soap 更改为至 Body .

  3. 我不是 XML 专家,但我认为您在命名空间方面做错了什么。稍微简化你的类型,这是一个工作示例:http://play.golang.org/p/957GWzfdvN

    package main

    import "fmt"
    import "encoding/xml"

    type MyRespEnvelope struct {
    XMLName xml.Name
    Body Body
    }

    type Body struct {
    XMLName xml.Name
    GetResponse completeResponse `xml:"activationPack_completeResponse"`
    }

    type completeResponse struct {
    XMLName xml.Name `xml:"activationPack_completeResponse"`
    Id string `xml:"Id,attr"`
    MyVar string `xml:"activationPack_completeResult"`
    }

    func main() {

    Soap := []byte(`<?xml version="1.0" encoding="UTF-8"?>
    <soap:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <soap:Body>
    <activationPack_completeResponse Id="http://tempuri.org/">
    <activationPack_completeResult xsi:type="xsd:string">Active</activationPack_completeResult>
    </activationPack_completeResponse>
    </soap:Body>
    </soap:Envelope>`)

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

    fmt.Println(res.Body, err)
    }

    注意:在我放在一起的代码中,我不使用指向结构的指针,而是使用结构本身。您可以使用任何一种,具体取决于您打算如何使用它,以及我猜您的偏好。

关于soap - 如何在 Golang 中解析 Soap Envelope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23623143/

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