如何在Golang中解析Soap Envelope?

时间:2014-05-13 05:01:06

标签: soap go nusoap

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

1.我有肥皂消息

<?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"

所以有人请告诉我如何申报我的结构,以便我能够解析肥皂信息。

1 个答案:

答案 0 :(得分:8)

  1. 您的XML格式错误,我认为这是一个糟糕的复制粘贴。我更正了,第4行:<activationPack_completeResponse"http://tempuri.org/"> - &gt; <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)
    }
    

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

相关问题