Answer the question
In order to leave comments, you need to log in
Go how to parse XML?
Let's say there is XML that needs to be deserialized
<stream:features>
<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
<mechanism>PLAIN</mechanism>
<mechanism>DIGEST-MD5</mechanism>
<mechanism>SCRAM-SHA-1</mechanism>
</mechanisms>
<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-one.net/en/ejabberd/' ver='NUUMEO3rKA30XLGO1FbA9EZT1rY='/>
<register xmlns='http://jabber.org/features/iq-register'/>
</stream:features>
type Mechanism struct {
XMLName xml.Name `xml:"mechanism"`
Mechanism string
}
type Features struct {
XMLName xml.Name `xml:"features"`
Mechanisms []Mechanism `xml:mechanisms`
}
Let's just say, I myself understand that this will not work, and indeed, when trying to unmarshal, a nil structure of type Features is obtained. Answer the question
In order to leave comments, you need to log in
Something like this: https://play.golang.org/p/yiKGZY4GZL
package main
import (
"encoding/xml"
"fmt"
"log"
)
type CNode struct {
Hash string `xml:"hash,attr"`
Node string `xml:"node,attr"`
}
type Features struct {
XMLName xml.Name `xml:"stream features"`
Mechanisms []string `xml:"mechanisms>mechanism"`
C CNode `xml:"c"`
}
func main() {
raw_data := []byte(`
<stream:features>
<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
<mechanism>PLAIN</mechanism>
<mechanism>DIGEST-MD5</mechanism>
<mechanism>SCRAM-SHA-1</mechanism>
</mechanisms>
<c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-one.net/en/ejabberd/' ver='NUUMEO3rKA30XLGO1FbA9EZT1rY='/>
<register xmlns='http://jabber.org/features/iq-register'/>
</stream:features>
`)
var v Features
err := xml.Unmarshal(raw_data, &v)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", v)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question