Marshal 一个 JSON Marshal 兼容映射到 XML

我有一张地图:[]map[string]string。


将结果填充到json.marshal()兼容的对象中。输出:


[

  {

    "key1": "val1",

    "key2": "val2"

  },

  {

    "randval3": "val1",

    "randval2": "xyz1"

    "randval1": "xyz3"

  },

  ...

]

但是,当我运行xml.marshal(). 我收到一个xml: unsupported type: map[string]string. 考虑到 XML 需要节点名称等这一事实,这似乎是合理的。所以我基本上要寻找的是一种获得方法:


<rootElement>

    <child>

        <key1>val1</key1>

        <key2>val1</key2>

    </child>

    <child>

        <randval3>val1</randval3>

        <randval2>xyz1</randval2>

        <randval1>xyz1</randval1>

    </child>

</rootElement>

但是我一直无法获得与兼容的“对象”xml.unmarshal()


墨色风雨
浏览 148回答 2
2回答

芜湖不芜

您可以声明一个自定义地图并让它实现xml.Marshaler接口。type mymap map[string]stringfunc (m mymap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {&nbsp; &nbsp; if err := e.EncodeToken(start); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; for key, val := range m {&nbsp; &nbsp; &nbsp; &nbsp; s := xml.StartElement{Name: xml.Name{Local: key}}&nbsp; &nbsp; &nbsp; &nbsp; if err := e.EncodeElement(val, s); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return e.EncodeToken(start.End())}type RootElement struct {&nbsp; &nbsp; XMLName&nbsp; xml.Name `xml:"rootElement"`&nbsp; &nbsp; Children []mymap&nbsp; `xml:"child"`}&nbsp;https://play.golang.com/p/0_qA9UUvhKVfunc main() {&nbsp; &nbsp; root := RootElement{Children: []mymap{&nbsp; &nbsp; &nbsp; &nbsp; {"key1": "val1", "key2": "val2"},&nbsp; &nbsp; &nbsp; &nbsp; {"randval1": "val1", "randval2": "xyz1", "randval3": "abc3"},&nbsp; &nbsp; }}&nbsp; &nbsp; data, err := xml.MarshalIndent(root, "", "&nbsp; ")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(string(data))}输出:<rootElement>&nbsp; <child>&nbsp; &nbsp; <key2>val2</key2>&nbsp; &nbsp; <key1>val1</key1>&nbsp; </child>&nbsp; <child>&nbsp; &nbsp; <randval3>abc3</randval3>&nbsp; &nbsp; <randval1>val1</randval1>&nbsp; &nbsp; <randval2>xyz1</randval2>&nbsp; </child></rootElement>

侃侃无极

对于 Marshalling/Unmarshalling 映射,您需要编写自己的Marshal()和Unmarshal()函数。下面是一个实现这些功能type Maps []map[string]string以及如何使用它们的示例。type Maps []map[string]stringtype xmlMapEntry struct {&nbsp; &nbsp; XMLName xml.Name&nbsp; &nbsp; Value&nbsp; &nbsp;string `xml:",chardata"`}func (m Maps) MarshalXML(e *xml.Encoder, start xml.StartElement) error {&nbsp; &nbsp; if len(m) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }&nbsp; &nbsp; err := e.EncodeToken(start)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; for _, ma := range m {&nbsp; &nbsp; &nbsp; &nbsp; for k, v := range ma {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.Encode(xmlMapEntry{XMLName: xml.Name{Local: k}, Value: v})&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return e.EncodeToken(start.End())}func (m *Maps) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {&nbsp; &nbsp; *m = Maps{}&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; var e xmlMapEntry&nbsp; &nbsp; &nbsp; &nbsp; err := d.Decode(&e)&nbsp; &nbsp; &nbsp; &nbsp; if err == io.EOF {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; } else if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; *m = append(*m, map[string]string{e.XMLName.Local: e.Value})&nbsp; &nbsp; }&nbsp; &nbsp; return nil}func main() {&nbsp; &nbsp; myarraymap := []map[string]string{&nbsp; &nbsp; &nbsp; &nbsp; 0: {"Key1": "val1"},&nbsp; &nbsp; &nbsp; &nbsp; 1: {"Key2": "val2"},&nbsp; &nbsp; &nbsp; &nbsp; 2: {"Randval3": "val1"},&nbsp; &nbsp; &nbsp; &nbsp; 3: {"Randval2": "xyz1"},&nbsp; &nbsp; &nbsp; &nbsp; 4: {"Randval1": "xyz2"},&nbsp; &nbsp; }&nbsp; &nbsp; // Encode to XML&nbsp; &nbsp; x, _ := xml.MarshalIndent(Maps(myarraymap), "", "&nbsp; ")&nbsp; &nbsp; fmt.Printf("my marshaled xml map: %v\n", string(x))&nbsp; &nbsp; // Decode back from XML&nbsp; &nbsp; var rm []map[string]string&nbsp; &nbsp; xml.Unmarshal(x, (*Maps)(&rm))&nbsp; &nbsp; fmt.Printf("my unmarshalled xml map: %v \n", rm)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go