我想将特定节点N的属性X的值解码到struct字段。像这样:

var data = `<A id="A_ID">
<B id="B_ID">Something</B>
</A>
`

type A struct {
    Id   string `xml:"id,attr"` // A_ID
    Name string `xml:"B.id,attr"` // B_ID
}

http://play.golang.org/p/U6daYJWVUX

就我能够check而言,Go不支持此功能。我是正确的,还是我在这里错过了什么?

最佳答案

在您的问题中,您没有提到B。我猜您需要将其attr解码为A.Name吗?如果是这样-您可以将A结构更改为以下形式:

type A struct {
    Id string `xml:"id,attr"` // A_ID
    Name  struct {
        Id string `xml:"id,attr"` // B_ID
    } `xml:"B"`
}

甚至更好-定义单独的B结构:
type A struct {
    Id string `xml:"id,attr"` // A_ID
    Name  B `xml:"B"`
}

type B struct {
    Id string `xml:"id,attr"` // B_ID
}

09-04 13:09