解析XML中的日期无法工作

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

Parsing date from XML does not work

问题

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

type customDate struct {
    time.Time
}

func (c *customDate) UnmarshalXml(d *xml.Decoder, start xml.StartElement) error {
    var v string
    if err := d.DecodeElement(&v, &start); err != nil {
        return err
    }

    loc, _ := time.LoadLocation("Europe/Moscow")
    prs, err := time.ParseInLocation("02.01.2006", v, loc)
    if err != nil {
        return err
    }

    *c = customDate{prs}
    return nil
}

在playground上的示例

英文:

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

type customDate struct {
	time.Time
}


func (c *customDate) UnmarshalXml(d *xml.Decoder, start xml.StartElement) error {
	var v string
	if err := d.DecodeElement(&v, &start); err != nil{
		return err
	}
	
	loc, _ := time.LoadLocation("Europe/Moscow")
	prs, err := time.ParseInLocation("02.01.2006", v, loc)
	if err != nil {
		return err
	}
	
	*c = customDate{prs}
	return nil
}

example on playground

答案1

得分: 2

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

package main

import (
	"encoding/xml"
	"fmt"
	"time"
)

type xmlSource struct {
	XMLName xml.Name `xml:"BicDBList"`
	Base    string   `xml:"Base,attr"`
	Items   []item   `xml:"item"`
}

// Item代表节点"item"的结构
type item struct {
	File string     `xml:"file,attr"`
	Date customDate `xml:"date,attr"`
}

type customDate struct {
	time.Time
}

func (c *customDate) UnmarshalXMLAttr(attr xml.Attr) error {
	loc, err := time.LoadLocation("Europe/Moscow")
	if err != nil {
		return err
	}
	prs, err := time.ParseInLocation("02.01.2006", attr.Value, loc)
	if err != nil {
		return err
	}

	c.Time = prs
	return nil
}

var data = []byte(`<BicDBList Base="/mcirabis/BIK/">
    <item file="bik_db_09122016.zip" date="09.12.2016"/>
    <item file="bik_db_08122016.zip" date="08.12.2016"/>
    <item file="bik_db_07122016.zip" date="07.12.2016"/>
    <item file="bik_db_06122016.zip" date="06.12.2016"/>
</BicDBList>`)

func main() {
	var sample xmlSource

	err := xml.Unmarshal(data, &sample)

	if err != nil {
		println(err.Error())
	}
	fmt.Printf("%#v\n", sample)
}

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

英文:

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

package main

import (
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
	&quot;time&quot;
)

type xmlSource struct {
	XMLName xml.Name `xml:&quot;BicDBList&quot;`
	Base    string   `xml:&quot;Base,attr&quot;`
	Items   []item   `xml:&quot;item&quot;`
}

// Item represent structure of node &quot;item&quot;
type item struct {
	File string     `xml:&quot;file,attr&quot;`
	Date customDate `xml:&quot;date,attr&quot;`
}

type customDate struct {
	time.Time
}

func (c *customDate) UnmarshalXMLAttr(attr xml.Attr) error {
	loc, err := time.LoadLocation(&quot;Europe/Moscow&quot;)
	if err != nil {
		return err
	}
	prs, err := time.ParseInLocation(&quot;02.01.2006&quot;, attr.Value, loc)
	if err != nil {
		return err
	}

	c.Time = prs
	return nil
}

var data = []byte(`&lt;BicDBList Base=&quot;/mcirabis/BIK/&quot;&gt;
    &lt;item file=&quot;bik_db_09122016.zip&quot; date=&quot;09.12.2016&quot;/&gt;
    &lt;item file=&quot;bik_db_08122016.zip&quot; date=&quot;08.12.2016&quot;/&gt;
    &lt;item file=&quot;bik_db_07122016.zip&quot; date=&quot;07.12.2016&quot;/&gt;
    &lt;item file=&quot;bik_db_06122016.zip&quot; date=&quot;06.12.2016&quot;/&gt;
&lt;/BicDBList&gt;`)

func main() {
	var sample xmlSource

	err := xml.Unmarshal(data, &amp;sample)

	if err != nil {
		println(err.Error())
	}
	fmt.Printf(&quot;%#v\n&quot;, sample)
}

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:

确定