I'm hoping to be able to do something like this...
/** source xml:
<things>
<thing type="Point">
<x>1</x>
<y>2</y>
</thing>
</things>
*/
type Thing interface { /* .. */ }
// note: Point would implement Thing
type Point struct {
X float64 `xml:"x"`
Y float64 `xml:"y"`
}
type SomeThings struct {
Things []Thing `xml:"thing"`
}
func UnmarshalThings(xml []byte) (some *SomeThings, err error) {
some = &SomeThings{}
err = xml.Unmarhal(xml, someThings)
return
}
Now, that won't work as-is. When unmarshalled there will be 1 "Thing" with a value of nil
. What I'm hoping is that there might be a way for me - even as a post-process step - to get the "type" attribute from <Thing>, call reflect.New to create the Point, and then unmarshal the inner XML into that.
Is something like this doable or is there a library out there someone has written to do this already?
Thanks!
评论:
silviucm:
stymiedcoder:
Thank you very much for this!
