douqie1852 2017-03-29 18:00
浏览 177
已采纳

golang:省略xml.Marshal中的struct名称

Trying to get a particular output for XML:

<RESPONSE>
    <DELIMITER value="09"></DELIMITER>
    <COLUMNS>Id FirstName   LastName    Company Initials</COLUMNS>
     <DATA>87734    Some Guy    Keller Williams Realty Empire   </DATA>
     <DATA>87739    Another Guy Level Group, Inc.   </DATA>
     <DATA>87741    Some Gal    Kian Realty NYC, LLC    </DATA>
</RESPONSE> 

instead I am getting:

<RESPONSE>
 <compactData>
        <DELIMITER value="09"></DELIMITER>
        <COLUMNS>Id FirstName   LastName    Company Initials</COLUMNS>
        <DATA>87734 Some Guy    Keller Williams Realty Empire   </DATA>
        <DATA>87739 Another Guy Level Group, Inc.   </DATA>
        <DATA>87741 Some Gal    Kian Realty NYC, LLC    </DATA>
 </compactData>
</RESPONSE> 

I am trying not to output the <compactData> tag at all. (everything else is a-ok)

my struct that I am marshal-ing looks like this:

type delimiter struct {
    Value string `xml:"value,attr"`
}

type innerData struct {
    Data string `xml:",innerxml"`
}

type compactData struct {
    Delimiter delimiter    `xml:"DELIMITER"`
    Columns   *innerData   `xml:"COLUMNS"`
    Records   []*innerData `xml:"DATA"`
}

I have tried putting in

XMLName   xml.Name

or

XMLName   xml.Name `xml:""`

to no avail- the xml.MarshalIndent just says 'yeah that looks like crap taking the struct name instead'.

I feel as though I just may need to reorganize the struct a bit, but I am stumped as to the magic structure needed.

Thanks!

Edit: the <RESPONSE> tag is added as the http.reponse is sent, after this formatting is done, trying to keep this signature the same as JSON and XML encoding...

like so:

func createResponse(obj interface{}, format string) (*Response, error) {
    var data []byte

    var f func(interface{}, string, string) ([]byte, error)

    switch format {
    case FORMAT_JSON:
        f = json.MarshalIndent
    case FORMAT_COMPACT:
        f = compact.MarshalIndent
    default:
        f = xml.MarshalIndent
    }
    data, err := f(obj, " ", "    ")
    if err != nil {
        log.Println(err)
        return nil, err
    }
    return &Response{Data: string(data)}, nil

}
  • 写回答

1条回答 默认 最新

  • dqypcghd381390 2017-03-29 18:35
    关注

    If RESPONSE is another struct you could embed compactData in it and let encoding/xml do what you want.

    type RESPONSE struct {
        compactData
    }
    

    https://play.golang.org/p/hN1hDY7pnV

    Update: Actually you can do what you want, if I understood you correctly, by implementing the xml.Marshaler interface. E.g. something like this: https://play.golang.org/p/Uwm0pP1pWh.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换