我想将特定节点 N 的属性 X 的值解码到结构字段。像这样:
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
}
关于xml - 节点 N 的 Go XML Unmarshalling 属性 X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244103/