解组XML:根据属性值使用不同的目标类型

I want to use a different type to unmarshal the XML content of a child node based on its parent's name attribute.

In the following example I have 2 children nodes with attributes "apple" and "peach". I would like to use type Apple when the attribute is "apple" and Peach when it's "peach". Basically Apple and Peach have very different structures, so that's the scenario. How would I accomplish that or what would be the suggested approach?

Here is the playground with a basic setup to the problem.

<element>
    <node name="apple">
        <apple>
            <color>red<color>
        </apple>
    </node>
    <node name="peach"> 
        <peach>
            <size>medium</size>
        </peach>
    </node>
</element>
var x = `...` // xml
type Element struct {
    Nodes []struct{
        Name string `xml:"name,attr"`
    } `xml:"node"`
    Apple Apple
    Peach Peach
}
type Apple struct { // use this struct if name is "apple"
    Color string 
} 
type Peach struct { // use this struct if name is "peach"
    Size string
}
func main() {
    e := Element{}
    err := xml.Unmarshal([]byte(x), &e)
    if err != nil {
        panic(err)
    }   
    fmt.Println(e.Apple.Color)
    fmt.Println(e.Peach.Size
}

You can simply iterate on the nodes of your Element type and create the Apple and Peach structures as you go, by switching on their Name attribute:

    for _, element := range e.Nodes {
        switch element.Name {
        case "apple":
            apples = append(apples, Apple{})
        case "peach":
            peaches = append(peaches, Peach{})
        }
    }

Here is a playground link.

Another, more complex solution (but also more elegant and practical) would be to implement your own UnmarshalXML method on your Element type, which would directly populate it with the proper types:

type Apple struct {
    Color string
}
type Peach struct {
    Size string
}

type Fruits struct {
    Apples  []Apple
    Peaches []Peach
}

type Element struct {
    XMLName xml.Name `xml:"element"`
    Nodes   []struct {
        Name  string `xml:"name,attr"`
        Apple struct {
            Color string `xml:"color"`
        } `xml:"apple"`
        Peach struct {
            Size string `xml:"size"`
        } `xml:"peach"`
    } `xml:"node"`
}

func (f *Fruits) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    var element Element
    d.DecodeElement(&element, &start)

    for _, el := range element.Nodes {
        switch el.Name {
        case "apple":
            f.Apples = append(f.Apples, Apple{
                Color: el.Apple.Color,
            })
        case "peach":
            f.Peaches = append(f.Peaches, Peach{
                Size: el.Peach.Size,
            })
        }
    }

    return nil
}

func main() {
    f := Fruits{}

    err := xml.Unmarshal([]byte(x), &f)
    if err != nil {
        panic(err)
    }

    fmt.Println("Apples:", f.Apples)
    fmt.Println("Peaches", f.Peaches)
}

Here is a playground link for this second solution

Result:

Apples: [{red}]
Peaches [{medium}]