2014-05-13 22 views
5

Tôi mới dùng golang và Soap và gặp rắc rối trong việc phân tích cú pháp xà phòng.Làm cách nào để phân tích Soap Envelope ở Golang?

1.Tôi có một thông báo Xà phòng

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

Bây giờ làm thế nào tôi nên unmarshal chúng trong golang những gì cần được khai struct tôi cho thẻ Soap Envelope.

Tôi có một số cấu trúc như sau:

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"` 
} 

Nhưng tôi nhận được lỗi như sau:

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" 

Vì vậy, một người nào đó xin vui lòng cho tôi biết làm thế nào tôi nên tuyên bố cấu trúc của tôi để tôi có thể phân tích cú pháp thông điệp xà phòng.

+1

Bạn có chắc tài liệu bạn đang cố phân tích cú pháp thực sự là XML không? Thông báo lỗi làm cho nó có vẻ như bạn đang cố phân tích lỗi (không phải XML) từ tập lệnh PHP –

+0

@JamesHenstridge Có tôi. Nhưng đối với kiến ​​thức của tôi trường nào khiến bạn cảm thấy rằng php đang trả về lỗi – user2383973

+1

Một phần của lỗi đọc 'args = [{name =" data ", value =" \ "\\ nNotice: Biến không xác định: khu vực trong/var/www/nusoap/dittotv.php trên dòng 25 \\ n ... ' –

Trả lời

7
  1. XML của bạn không đúng định dạng, tôi cho rằng đó là bản sao dán sai. Tôi đã sửa nó, dòng 4: <activationPack_completeResponse"http://tempuri.org/"> -><activationPack_completeResponse Id="http://tempuri.org/">

  2. Loại của bạn sai. trong số MyRespEnvelope bạn gọi số Body struct Soap. Mà không xác định tên xml của nó, bạn sẽ không nhận được bất cứ điều gì. Sửa chữa dễ dàng hơn là thay đổi tên từ Soap thành Body.

  3. Tôi không phải là chuyên gia về XML, nhưng tôi nghĩ bạn đang làm điều gì đó sai với không gian tên. đơn giản hóa các loại của bạn một chút, đây là một ví dụ làm việc: 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) 
    } 
    

    Lưu ý: Trong đoạn code tôi đặt lại với nhau, tôi không sử dụng con trỏ đến struct nhưng các cấu trúc bản thân. Bạn có thể sử dụng hoặc tùy thuộc vào cách bạn định sử dụng và tùy chọn của bạn.

Các vấn đề liên quan