解析XML中的日期无法工作

huangapple go评论125阅读模式
英文:

Parsing date from XML does not work

问题

如果我使用简单的值执行time.Parse(),一切都正常,但解析XML时出现问题。

  1. type customDate struct {
  2. time.Time
  3. }
  4. func (c *customDate) UnmarshalXml(d *xml.Decoder, start xml.StartElement) error {
  5. var v string
  6. if err := d.DecodeElement(&v, &start); err != nil {
  7. return err
  8. }
  9. loc, _ := time.LoadLocation("Europe/Moscow")
  10. prs, err := time.ParseInLocation("02.01.2006", v, loc)
  11. if err != nil {
  12. return err
  13. }
  14. *c = customDate{prs}
  15. return nil
  16. }

在playground上的示例

英文:

If I execute time.Parse() with simple values - then everything is fine, but parsing XML does not.

  1. type customDate struct {
  2. time.Time
  3. }
  4. func (c *customDate) UnmarshalXml(d *xml.Decoder, start xml.StartElement) error {
  5. var v string
  6. if err := d.DecodeElement(&v, &start); err != nil{
  7. return err
  8. }
  9. loc, _ := time.LoadLocation("Europe/Moscow")
  10. prs, err := time.ParseInLocation("02.01.2006", v, loc)
  11. if err != nil {
  12. return err
  13. }
  14. *c = customDate{prs}
  15. return nil
  16. }

example on playground

答案1

得分: 2

date是一个XML属性,而不是元素。因此,您必须实现xml.UnmarshalerAttr接口,而不是xml.Unmarshaler接口:

  1. package main
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "time"
  6. )
  7. type xmlSource struct {
  8. XMLName xml.Name `xml:"BicDBList"`
  9. Base string `xml:"Base,attr"`
  10. Items []item `xml:"item"`
  11. }
  12. // Item代表节点"item"的结构
  13. type item struct {
  14. File string `xml:"file,attr"`
  15. Date customDate `xml:"date,attr"`
  16. }
  17. type customDate struct {
  18. time.Time
  19. }
  20. func (c *customDate) UnmarshalXMLAttr(attr xml.Attr) error {
  21. loc, err := time.LoadLocation("Europe/Moscow")
  22. if err != nil {
  23. return err
  24. }
  25. prs, err := time.ParseInLocation("02.01.2006", attr.Value, loc)
  26. if err != nil {
  27. return err
  28. }
  29. c.Time = prs
  30. return nil
  31. }
  32. var data = []byte(`<BicDBList Base="/mcirabis/BIK/">
  33. <item file="bik_db_09122016.zip" date="09.12.2016"/>
  34. <item file="bik_db_08122016.zip" date="08.12.2016"/>
  35. <item file="bik_db_07122016.zip" date="07.12.2016"/>
  36. <item file="bik_db_06122016.zip" date="06.12.2016"/>
  37. </BicDBList>`)
  38. func main() {
  39. var sample xmlSource
  40. err := xml.Unmarshal(data, &sample)
  41. if err != nil {
  42. println(err.Error())
  43. }
  44. fmt.Printf("%#v\n", sample)
  45. }

您可以在这里找到完整的代码示例。

英文:

date is an XML attribute, not an element. Therefore, you must implement the xml.UnmarshalerAttr interface rather than xml.Unmarshaler:

  1. package main
  2. import (
  3. &quot;encoding/xml&quot;
  4. &quot;fmt&quot;
  5. &quot;time&quot;
  6. )
  7. type xmlSource struct {
  8. XMLName xml.Name `xml:&quot;BicDBList&quot;`
  9. Base string `xml:&quot;Base,attr&quot;`
  10. Items []item `xml:&quot;item&quot;`
  11. }
  12. // Item represent structure of node &quot;item&quot;
  13. type item struct {
  14. File string `xml:&quot;file,attr&quot;`
  15. Date customDate `xml:&quot;date,attr&quot;`
  16. }
  17. type customDate struct {
  18. time.Time
  19. }
  20. func (c *customDate) UnmarshalXMLAttr(attr xml.Attr) error {
  21. loc, err := time.LoadLocation(&quot;Europe/Moscow&quot;)
  22. if err != nil {
  23. return err
  24. }
  25. prs, err := time.ParseInLocation(&quot;02.01.2006&quot;, attr.Value, loc)
  26. if err != nil {
  27. return err
  28. }
  29. c.Time = prs
  30. return nil
  31. }
  32. var data = []byte(`&lt;BicDBList Base=&quot;/mcirabis/BIK/&quot;&gt;
  33. &lt;item file=&quot;bik_db_09122016.zip&quot; date=&quot;09.12.2016&quot;/&gt;
  34. &lt;item file=&quot;bik_db_08122016.zip&quot; date=&quot;08.12.2016&quot;/&gt;
  35. &lt;item file=&quot;bik_db_07122016.zip&quot; date=&quot;07.12.2016&quot;/&gt;
  36. &lt;item file=&quot;bik_db_06122016.zip&quot; date=&quot;06.12.2016&quot;/&gt;
  37. &lt;/BicDBList&gt;`)
  38. func main() {
  39. var sample xmlSource
  40. err := xml.Unmarshal(data, &amp;sample)
  41. if err != nil {
  42. println(err.Error())
  43. }
  44. fmt.Printf(&quot;%#v\n&quot;, sample)
  45. }

https://play.golang.org/p/U56qfEOe-A

huangapple
  • 本文由 发表于 2016年12月14日 21:01:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/41143350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定