Golang XML Unmarshal和time.Time字段 [英] Golang XML Unmarshal and time.Time fields

查看:162
本文介绍了Golang XML Unmarshal和time.Time字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过REST API检索的XML数据,我将其解组为一个GO结构。其中一个字段是日期字段,但由API返回的日期格式与默认的time.Time解析格式不匹配,因此解组失败。



任何方式来指定解组函数使用哪种日期格式的时间。解析时间?我想使用正确定义的类型,并使用字符串来保存日期时间字段感觉不对。



示例结构:

 类型事务结构{

Id int64`xml:sequencenumber`
ReferenceNumber string`xml:ourref`
描述字符串`xml:description`
类型字符串`xml:type`
CustomerID字符串`xml:namecode`
DateEntered time.Time`xml:enterdate `//这是所讨论的字段
Gross float64`xml:gross`
Container TransactionDetailContainer`xml:subfile`
}

返回的日期格式为yyyymmdd。

我有同样的问题。



time.Time 不符合 xml.Unmarshaler 界面。而且你不能指定一个日期格式。



如果你以后不想处理解析,而你更愿意让 xml.encoding 这样做,一种解决方案是用匿名 time.Time 字段创建一个结构并实现您自己的 UnmarshalXML <

 类型事务结构{
// ...
DateEntered customTime`xml:enterdate`//使用您自己的类型满足UnmarshalXML
// ...
}

类型customTime结构{
time.Time
}

func(c * customTime)UnmarshalXML(d * xml.Decoder,start xml.StartElement)error {
const shortForm =20060102// yyyymmdd日期格式
var v字符串
d.DecodeElement(& v,& start)
解析,err:= time.Parse(shortForm,v)
if err!= nil {
return err
}
* c = customTime {parse}
return nil
}

如果您的XML元素使用attribut作为日期,则必须以相同的方式实现UnmarshalXMLAttr。 http://play.golang.org/p/EFXZNsjE4a


I have XML data I am retrieving via a REST API that I am unmarshal-ing into a GO struct. One of the fields is a date field, however the date format returned by the API does not match the default time.Time parse format and thus the unmarshal fails.

Is there any way to specify to the unmarshal function which date format to use in the time.Time parsing? I'd like to use properly defined types and using a string to hold a datetime field feels wrong.

Sample struct:

type Transaction struct {

    Id int64 `xml:"sequencenumber"`
    ReferenceNumber string `xml:"ourref"`
    Description string `xml:"description"`
    Type string `xml:"type"`
    CustomerID string `xml:"namecode"`
    DateEntered time.Time `xml:"enterdate"` //this is the field in question
    Gross float64 `xml:"gross"`
    Container TransactionDetailContainer `xml:"subfile"`
}

The date format returned is "yyyymmdd".

解决方案

I had the same problem.

time.Time doesn't satisfy the xml.Unmarshaler interface. And you can not specify a date fomat.

If you don't want to handle the parsing afterward and you prefer to let the xml.encoding do it, one solution is to create a struct with an anonymous time.Time field and implement your own UnmarshalXML with your custom date format.

type Transaction struct {
    //...
    DateEntered     customTime     `xml:"enterdate"` // use your own type that satisfies UnmarshalXML
    //...
}

type customTime struct {
    time.Time
}

func (c *customTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    const shortForm = "20060102" // yyyymmdd date format
    var v string
    d.DecodeElement(&v, &start)
    parse, err := time.Parse(shortForm, v)
    if err != nil {
        return err
    }
    *c = customTime{parse}
    return nil
}

If your XML element uses an attribut as a date, you have to implement UnmarshalXMLAttr the same way.

See http://play.golang.org/p/EFXZNsjE4a

这篇关于Golang XML Unmarshal和time.Time字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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