2014-05-13 85 views
5

我是golang和soap新手,在解析soap msg時遇到了麻煩。如何解析Golang中的肥皂信封?

1.I有一個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信封結構聲明。

我有如下一些結構:

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

您確定您要解析的文檔實際上是XML嗎?錯誤消息使得它聽起來像你試圖從PHP腳本解析(非XML)錯誤 –

+0

@JamesHenstridge是的,我是。但是據我所知,什麼字段會讓你覺得php返回錯誤 – user2383973

+1

錯誤的一部分讀取了'args = [{name =「data」,value =「\」\\ n注意:未定義的變量:/ var/www/nusoap/dittotv.php on line 25 \\ n ...' –

回答

7
  1. 您的XML格式錯誤,我認爲這是一個錯誤的複製粘貼。我糾正了它,第4行:<activationPack_completeResponse"http://tempuri.org/"> - ><activationPack_completeResponse Id="http://tempuri.org/">

  2. 您的類型錯了。在MyRespEnvelope中您可以撥打Body struct 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) 
    } 
    

    注:在我放在一起的代碼,我不使用指針結構,但結構本身。您可以根據您打算使用它的方式以及您的偏好進行使用。