如何解析Golang中的Soap Envelope



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

    注意:在我放在一起的代码中,我没有使用指向结构体的指针,而是使用结构体本身。你可以根据你打算如何使用它,我猜是你的偏好来使用。

最新更新