去xml编组属性没有多余的结构

时间:2017-05-29 13:48:00

标签: xml go marshalling

我正在与客户端讨论XML API。一切都工作得很好,但我觉得编码器(或我没有理解它)迫使我使用我真的不需要的其他结构。

我需要发送的请求类型的示例如下:

<command_name>
    <prop_1>p1</prop_1>
    <prop_2>p2</prop_2>
    <prop_3 id="prop_3_id"></prop_3>
</command_name>

值得注意的是,prop_3将永远不会在节点内有任何内容,而id将是唯一存在的属性

为了对这个回应做出回应,我目前正在使用以下两种结构:

type RefID struct {
    ID string `xml:"id,attr,omitempty"`
}

type CommandName struct {
    XMLName       xml.Name `xml:"command_name"`
    Prop1         string   `xml:"prop_1"`
    Prop2         bool     `xml:"prop_2,omitempty"`
    Prop3         RefID    `xml:"prop_3,omitempty"`
}

我想要的是能够将prop_3 id传递给请求而不必使用RefID结构。我试过这样的东西,但它似乎不适用于属性:

Prop3  string `xml:"prop_3>id,attr,omitempty"`

我知道这看起来很小但是只会让任何消费我客户的人生活更轻松,如果有人可以肯定地确认这是不可能的,那么至少我可以继续并删除我的代码旁边的脏@TODO。

0 个答案:

没有答案