如何将混合元素的xml序列映射到go结构?

fai*_*ain 5 xml choice sequence go

我试图加载一个包含无限混合元素序列的XML文件(在XSD中的序列中选择)该文件看起来像这样:

<RootNode>
    <ElementB>...</ElementB>
    <ElementA>...</ElementA>
    <ElementA>...</ElementA>
    <ElementC>...</ElementC>
    <ElementB>...</ElementB>
    <ElementA>...</ElementA>
    <ElementB>...</ElementB>
</RootNode>
Run Code Online (Sandbox Code Playgroud)

我使用xml.Unmarshal初始化并填充这些结构:

type RootNode struct {
    ElementA []ElementA
    ElementB []ElementB
    ElementC []ElementC
}

type ElementA struct {
}

type ElementB struct {
}

type ElementC struct {
}
Run Code Online (Sandbox Code Playgroud)

我在这里有工作例子http://play.golang.org/p/ajIReJS35F.问题是我需要知道原始序列中元素的索引.通过该描述,此信息将丢失.

有没有办法在同一个数组中加载ElementA,ElementB或ElementC类型的元素?更一般地说,将混合元素列表映射到go结构的最佳方法是什么?

Ain*_*r-G 5

xml:",any"您可以在根节点上使用标签,然后将其余节点解组到具有XMLName如下字段的结构中:

type RootNode struct {
    Elements []Element `xml:",any"`
}

type Element struct {
    XMLName xml.Name
}
Run Code Online (Sandbox Code Playgroud)

更多内容请xml:",any"参阅XMLName 此处

游乐场示例: http: //play.golang.org/p/Vl9YI8GG1E