普通网友 2015-11-02 20:54
浏览 391

Golang xml.Unmarshal接口类型

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>

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

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).

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

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-11-04 09:17
    关注

    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
    }
    

    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).

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler