转到节点N的XML Unmarshalling属性X. [英] Go XML Unmarshalling attribute X of node N

查看:103
本文介绍了转到节点N的XML Unmarshalling属性X.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to unmarshall the value of an attribute X of specific node N to a struct field. Something like this:

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

据我能够检查,Go不支持此功能。我是否正确,或者我在这里丢失了什么?

As far as I was able to check this is not supported by Go. Am I correct, or am I missing something here?

推荐答案

在你的问题中,你没有提及 B 。我猜你需要将它的attr解组成 A.Name ?如果是这样的话 - 你可以把你的A结构变成这样的东西:

In your question you are not mentioning B. I'm guessing that you need to unmarshal its attr into A.Name? If so - you could change your A struct to something like this:

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

或者甚至更好 - 定义单独的B结构:

Or maybe even better - define separate B struct:

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

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

这篇关于转到节点N的XML Unmarshalling属性X.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆