将单个字段分别编组为两个标签在golang中

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

Marshal single field into two tags in golang

问题

尝试理解一种为结构化为以下形式的XML创建自定义编组器的方法:

<!-- language: lang-xml -->
    <Appointment>
    <Date>2004-12-22</Date>
    <Time>14:00</Time>
    </Appointment>

我在考虑这样的解决方案:

<!-- language: lang-go -->
    type Appointment struct {
        DateTime time.Time `xml:"???"`
    }

问题是,我应该在"???"的位置放什么,才能将一个字段保存到两个不同的XML标签中?

英文:

Trying to understand a way to create custom marshaller for xml structured as:

<!-- language: lang-xml -->

&lt;Appointment&gt;
&lt;Date&gt;2004-12-22&lt;/Date&gt;
&lt;Time&gt;14:00&lt;/Time&gt;
&lt;/Appointment&gt;

I'm thinking of something like:

<!-- language: lang-go -->

type Appointment struct {
    DateTime time.Time `xml:&quot;???&quot;`
}

Question is, what would i put instead of ??? to have a single field be saved into two different xml tags?

答案1

得分: 3

复杂的编组/解组行为通常需要满足Marshal/Unmarshal接口(这对于XML、JSON和类似设置的Go类型都是如此)。

您需要使用MarshalXML()函数满足xml.Marshaler接口,类似以下示例:

package main

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

type Appointment struct {
    DateTime time.Time
}

type appointmentExport struct {
    XMLName struct{} `xml:"appointment"`
    Date    string   `xml:"date"`
    Time    string   `xml:"time"`
}

func (a *Appointment) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    n := &appointmentExport{
        Date: a.DateTime.Format("2006-01-02"),
        Time: a.DateTime.Format("15:04"),
    }
    return e.Encode(n)
}

func main() {
    a := &Appointment{time.Now()}
    output, _ := xml.MarshalIndent(a, "", "    ")
    fmt.Println(string(output))
}

// 输出:
// <appointment>
//     <date>2016-04-15</date>
//     <time>17:43</time>
// </appointment>

以上代码将输出XML格式的字符串。

英文:

Complex marshaling/unmarshaling behavior generally requires satisfying a Marshal/Unmarshal interface (this is of true of XML, JSON, and similarly setup types in go).

You'd need to satisfy the xml.Marshaler interface with a MarshalXML() function, something like the following:

package main

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

type Appointment struct {
    DateTime time.Time
}

type appointmentExport struct {
    XMLName struct{} `xml:&quot;appointment&quot;`
    Date    string   `xml:&quot;date&quot;`
    Time    string   `xml:&quot;time&quot;`
}

func (a *Appointment) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    n := &amp;appointmentExport{
        Date: a.DateTime.Format(&quot;2006-01-02&quot;),
        Time: a.DateTime.Format(&quot;15:04&quot;),
    }
    return e.Encode(n)
}

func main() {
    a := &amp;Appointment{time.Now()}
    output, _ := xml.MarshalIndent(a, &quot;&quot;, &quot;    &quot;)
    fmt.Println(string(output))
}

// prints:
// &lt;appointment&gt;
//     &lt;date&gt;2016-04-15&lt;/date&gt;
//     &lt;time&gt;17:43&lt;/time&gt;
// &lt;/appointment&gt;

答案2

得分: 0

快速猜测,你不能。你应该使用你的Appointment类型实现xml.Marshaler接口...

英文:

Quick guess, you can't. You should implement the xml.Marshaler interface with your Appointment type…

huangapple
  • 本文由 发表于 2016年4月13日 20:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/36599180.html
匿名

发表评论

匿名网友

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

确定