如何在Golang中解析Soap Envelope?

use*_*973 5 soap nusoap go

我是golang和Soap的新手,在解析soap msg方面遇到了麻烦.

我有肥皂消息

<?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>
Run Code Online (Sandbox Code Playgroud)

现在我应该如何在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"`
} 
Run Code Online (Sandbox Code Playgroud)

但我收到的错误如下:

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"
Run Code Online (Sandbox Code Playgroud)

所以有人请告诉我如何声明我的结构,以便我能够解析肥皂信息.

Dea*_*baz 8

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

  2. 你的类型错了.在MyRespEnvelope你调用Body结构Soap.如果没有定义它的xml名称,你就不会得到任何东西.更简单的解决方法是将名称从更改SoapBody.

  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)
    }
    
    Run Code Online (Sandbox Code Playgroud)

    注意:在我放在一起的代码中,我不使用指向结构的指针,而是使用结构本身.你可以根据你打算使用它的方式和你的偏好来使用.