2014-05-13 10 views
5

Sono nuovo di Golang e Soap e ho problemi nell'analisi del sapone msg.Come analizzare Soap Envelope in Golang?

1.I hanno un messaggio 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> 

Ora come non li ho Unmarshal in golang quella che dovrebbe essere la mia dichiarazione di struct per tag Soap busta.

ho qualche struttura come di seguito:

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

Ma sto errore ottenendo come di seguito:

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" 

Quindi, per favore qualcuno mi dica come devo dichiaro la mia struttura in modo che io sono in grado di analizzare il messaggio di sapone.

+1

Sei sicuro che il documento che stai cercando di analizzare sia in realtà XML? Il messaggio di errore fa sembrare che tu stia cercando di analizzare un errore (non XML) da uno script PHP –

+0

@JamesHenstridge Sì, lo sono. Ma per quanto ne so, quale campo ti fa sentire che php sta restituendo l'errore – user2383973

+1

Parte dell'errore legge 'args = [{name =" data ", value =" \ "\\ nNota: Variabile non definita: area in/var/www/nusoap/dittotv.php on line 25 \\ n ... ' –

risposta

7
  1. Il tuo XML non era corretto, presumo che si tratti di un brutto copia-incolla. L'ho corretto, riga 4: <activationPack_completeResponse"http://tempuri.org/"> -><activationPack_completeResponse Id="http://tempuri.org/">

  2. I tuoi tipi erano sbagliati. in MyRespEnvelope si chiama la struttura BodySoap. Senza definire il suo nome xml non otterrai nulla. Una soluzione più semplice è quella di cambiare il nome da Soap a Body.

  3. Non sono un esperto di XML, ma penso che stiate facendo qualcosa di sbagliato con i namespace. semplificando i tipi di un po ', ecco un esempio di lavoro: 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) 
    } 
    

    Nota: Nel codice ho messo insieme, io non uso puntatore per le strutture, ma le struct stessi. Puoi usare l'una o l'altra a seconda di come intendi usarla e le tue preferenze credo.

Problemi correlati