Marshall在Go中映射到XML

时间:2015-06-19 02:03:36

标签: xml go marshalling

我正在尝试将地图输出为XML数据,但是我收到以下错误:

xml: unsupported type: map[string]int

编组地图适用于JSON,所以我不明白为什么它对XML不起作用。使用Struct真的是唯一的方法吗?

3 个答案:

答案 0 :(得分:10)

我最后使用Dave C

建议的xml.Marshaler来解决这个问题
// StringMap is a map[string]string.
type StringMap map[string]string

// StringMap marshals into XML.
func (s StringMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {

    tokens := []xml.Token{start}

    for 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})

    for _, t := range tokens {
        err := e.EncodeToken(t)
        if err != nil {
            return err
        }
    }

    // flush to ensure tokens are written
    err := e.Flush()
    if err != nil {
        return err
    }

    return nil
}

来源:https://gist.github.com/jackspirou/4477e37d1f1c043806e0

现在可以通过简单地调用

来编组地图
output, err := xml.MarshalIndent(data, "", "  ")

答案 1 :(得分:2)

您可以编组和解组地图,但是您需要为地图编写自定义MarshalXML和UnmarshalXML函数,并为您提供映射类型以将这些函数附加到。

这是一个编组和解组的示例,其中地图中的键和值是字符串。您只需将值的编组更改为int =>字符串并返回unmarshal: https://play.golang.org/p/4Z2C-GF0E7

package main

import (
    "encoding/xml"
    "fmt"
    "io"
)

type Map map[string]string

type xmlMapEntry struct {
    XMLName xml.Name
    Value   string `xml:",chardata"`
}

// MarshalXML marshals the map to XML, with each key in the map being a
// tag and it's corresponding value being it's contents.
func (m Map) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    if len(m) == 0 {
        return nil
    }

    err := e.EncodeToken(start)
    if err != nil {
        return err
    }

    for k, v := range m {
        e.Encode(xmlMapEntry{XMLName: xml.Name{Local: k}, Value: v})
    }

    return e.EncodeToken(start.End())
}

// UnmarshalXML unmarshals the XML into a map of string to strings,
// creating a key in the map for each tag and setting it's value to the
// tags contents.
//
// The fact this function is on the pointer of Map is important, so that
// if m is nil it can be initialized, which is often the case if m is
// nested in another xml structurel. This is also why the first thing done
// on the first line is initialize it.
func (m *Map) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    *m = Map{}
    for {
        var e xmlMapEntry

        err := d.Decode(&e)
        if err == io.EOF {
            break
        } else if err != nil {
            return err
        }

        (*m)[e.XMLName.Local] = e.Value
    }
    return nil
}

func main() {
    // The Map
    m := map[string]string{
        "key_1": "Value One",
        "key_2": "Value Two",
    }
    fmt.Println(m)

    // Encode to XML
    x, _ := xml.MarshalIndent(Map(m), "", "  ")
    fmt.Println(string(x))

    // Decode back from XML
    var rm map[string]string
    xml.Unmarshal(x, (*Map)(&rm))
    fmt.Println(rm)
}

答案 2 :(得分:0)

我认为因为XML节点是排序的,但是map不是。检查this Marshal handles an array or slice by marshalling each of the elements.