Golang xml.Unmarshal接口类型 [英] Golang xml.Unmarshal interface types

查看:55
本文介绍了Golang xml.Unmarshal接口类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在golang中使用 xml 包,在解组非均匀类型列表时遇到了麻烦.考虑以下XML文档,该XML文档的嵌套元素是非同类类型的列表:

Using the xml package in golang I'm having trouble unmarshalling a list of non-homogenous types. Consider the following XML document whose nested elements are a list of non-homogenous types:

<mydoc>
  <foo>Foo</foo>
  <bar>Bar</bar>
  <foo>Another Foo</foo>
  <foo>Foo #3</foo>
  <bar>Bar 2</bar>
</mydoc>

以及以下用于测试XML取消/编组的golang代码(也在此处转到游乐场):

And the following golang code to test XML un/marshalling (also here on the go playground):

package main

import "encoding/xml"
import "fmt"

const sampleXml = `
<mydoc>
  <foo>Foo</foo>
  <bar>Bar</bar>
  <foo>Another Foo</foo>
  <foo>Foo #3</foo>
  <bar>Bar 2</bar>
</mydoc>
`

type MyDoc struct {
  XMLName xml.Name `xml:"mydoc"`
  Items   []Item
}

type Item interface {
  IsItem()
}

type Foo struct {
  XMLName xml.Name `xml:"foo"`
  Name    string   `xml:",chardata"`
}

func (f Foo) IsItem() {}

type Bar struct {
  XMLName xml.Name `xml:"bar"`
  Nombre  string   `xml:",chardata"`
}

func (b Bar) IsItem() {}

func main() {
  doMarshal()
  doUnmarshal()
}

func doMarshal() {
  myDoc := MyDoc{
    Items: []Item{
      Foo{Name: "Foo"},
      Bar{Nombre: "Bar"},
      Foo{Name: "Another Foo"},
      Foo{Name: "Foo #3"},
      Bar{Nombre: "Bar 2"},
    },
  }
  bytes, err := xml.MarshalIndent(myDoc, "", "  ")
  if err != nil {
    panic(err)
  }
  // Prints an XML document just like "sampleXml" above.
  println(string(bytes))
}

func doUnmarshal() {
  myDoc := MyDoc{}
  err := xml.Unmarshal([]byte(sampleXml), &myDoc)
  if err != nil {
    panic(err)
  }
  // Fails to unmarshal the "Item" elements into their respective structs.
  fmt.Printf("ERR: %#v", myDoc)
}

您将看到 doMarshal()产生了我期望的确切XML文档;但是, doUnmarshal()无法将"Item"元素反序列化为它们各自的结构.我尝试了一些更改,但似乎没有使它们正确解组的方法(为 myDoc.Items 创建存储,将"Items"的类型更改为 [] * Item [和其他],摆弄XML标记等).

You'll see that doMarshal() produces the exact XML document I expect; however, doUnmarshal() fails to deserialize the "Item" elements into their respective structs. I've tried a few changes but nothing seems to get them to unmarshal properly (creating storage for myDoc.Items, changing the type of "Items" to []*Item [and others], fiddling with the XML tags, etc).

有什么想法如何获取 xml.Unmarshal(...)来反序列化不相关类型的元素列表吗?

Any ideas how to get xml.Unmarshal(...) to deserialize a list of elements of unrelated types?

推荐答案

正如其他注释所指出的,解码器在没有一些帮助的情况下无法处理接口字段.在容器上实现 xml.Unmarshaller 将使其按您的意愿进行操作(在

As pointed out by other comments, the decoder cannot deal with interface fields without some help. Implementing xml.Unmarshaller on the container will make it do what you want (full working example on the playground):

func (md *MyDoc) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    md.XMLName = start.Name
    // grab any other attrs

    // decode inner elements
    for {
        t, err := d.Token()
        if err != nil {
            return err
        }
        var i Item
        switch tt := t.(type) {
        case xml.StartElement:
            switch tt.Name.Local {
            case "foo":
                i = new(Foo) // the decoded item will be a *Foo, not Foo!
            case "bar":
                i = new(Bar)
                // default: ignored for brevity
            }
            // known child element found, decode it
            if i != nil {
                err = d.DecodeElement(i, &tt)
                if err != nil {
                    return err
                }
                md.Items = append(md.Items, i)
                i = nil
            }
        case xml.EndElement:
            if tt == start.End() {
                return nil
            }
        }

    }
    return nil
}

这只是@evanmcdonnal建议的实现.所有这些操作是根据下一个令牌的名称实例化适当的 Item ,然后使用它调用 d.DecodeElement()(即让xml解码器完成繁重的工作)).

This is just an implementation of what @evanmcdonnal suggests. All this does is instantiate the proper Item based on the name of the next Token, then call d.DecodeElement() with it (i.e. let the xml decoder do the heavy lifting).

请注意,未编组的 Items 是指针.如果需要值,您需要做更多的工作.为了正确处理错误或意外的输入数据,还需要对其进行扩展.

Note that the unmarshalled Items are pointers. You'll need to do some more work if you want values. This also needs to be expanded some more for proper handling of errors or unexpected input data.

这篇关于Golang xml.Unmarshal接口类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆