如何解析Golang中的Soap信封?

如何解析Golang中的Soap信封?,soap,go,nusoap,Soap,Go,Nusoap,我是golang和Soap的新手,在解析Soap消息时遇到了麻烦 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

我是golang和Soap的新手,在解析Soap消息时遇到了麻烦

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>
但我得到的错误如下:

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"
错误:名称空间中应包含元素http://schemas.xmlsoap.org/soap/envelope/ 但是让soap*停止,reason=“end stepping range”,frame={addr=“0x000000000040121”,func=“main.unmarshalfirstdito”,args=[{name=“data”,value=“\”\\n注释:未定义变量:第25行/var/www/nusoap/dittotv.php中的区域\\n\\n注释:未定义变量:第27行/var/www/nusoap/dittotv.php中的区域\\n\\n注释:未定义变量:第27行/var/www/nu\“…”中的区域,文件=“/media/winshare/Golang/WorkSpace/src/dittotv/ditto.go”,全名=“/media/winshare/Golang/WorkSpace/src/dittotv/ditto.go”,line=“60”},thread id=“1”,stopped threads=“all”,core=“0”
所以有人请告诉我应该如何声明我的结构,以便能够解析soap消息

  • 您的XML格式不正确,我认为这是一个错误的复制粘贴。我更正了,第4行:

    主程序包
    输入“fmt”
    导入“编码/xml”
    类型myrespevelope结构{
    xml.Name
    身体
    }
    类型体结构{
    xml.Name
    GetResponse completeResponse`xml:“激活包_completeResponse”`
    }
    类型completeResponse结构{
    XMLName xml.Name`xml:“activationPack_completeResponse”`
    Id字符串`xml:“Id,attr”`
    MyVar字符串`xml:“activationPack_completeResult”`
    }
    func main(){
    Soap:=[]字节(`
    活跃的
    `)
    res:=&myrespevelope{}
    err:=xml.Unmarshal(Soap,res)
    fmt.Println(实体,错误)
    }
    
    注意:在我编写的代码中,我没有使用指向结构的指针,而是使用结构本身。你可以使用任何一种,这取决于你打算如何使用它,以及你的偏好


  • 您确定要解析的文档实际上是XML吗?错误消息听起来像是您试图解析来自PHP的(非XML)错误script@JamesHenstridge是的,我是。但就我所知,什么字段让您感觉php正在返回错误部分错误读取
    args=[{name=“data”,value=“\”\\注意:未定义变量:第25行/var/www/nusoap/dittotv.php中的区域\\n..
    这不是XML名称空间的工作方式。为了在元素和属性上使用
    soap:
    前缀,必须有一个
    xmlns:soap
    属性将前缀绑定到名称空间URI。
    xmlns:soap-ENV
    属性允许您例如,
    。可能尝试将文档馈送到
    xmllint
    工具以验证其有效性。您是否尝试通过
    xmllint
    运行文档以查看其是否实际有效?
    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)
    }