Marshall map to XML in Go -
i'm trying output map xml data, receive following error:
xml: unsupported type: map[string]int
marshalling maps works fine json don't why wouldn't work same xml. using struct way?
i ended solving using xml.marshaler suggested dave c
// stringmap map[string]string. type stringmap map[string]string // stringmap marshals xml. func (s stringmap) marshalxml(e *xml.encoder, start xml.startelement) error { tokens := []xml.token{start} key, value := range s { t := xml.startelement{name: xml.name{"", key}} tokens = append(tokens, t, xml.chardata(value), xml.endelement{t.name}) } tokens = append(tokens, xml.endelement{start.name}) _, t := range tokens { err := e.encodetoken(t) if err != nil { return err } } // flush ensure tokens written err := e.flush() if err != nil { return err } return nil }
source: https://gist.github.com/jackspirou/4477e37d1f1c043806e0
now map can marshalled calling
output, err := xml.marshalindent(data, "", " ")
Comments
Post a Comment