How to add an XML attribute to an element in Go?

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

How to add an XML attribute to an element in Go?

问题

我正在使用Go语言的encoding/xml包和编码器示例代码

虽然我能够生成可用的XML,但我无法添加所有需要的属性。

以温度报告为例,我需要的是这样的结构:

<environment>
  <temperature type="float" units="c">-11.3</temperature>
</environment>

我的结构体如下所示:

type climate struct {
	XMLName		xml.Name	`xml:"environment"`
	Temperature	string		`xml:"temperature"`
    Type		string		`xml:"type,attr"`
    Units		string		`xml:"units,attr"`
}

但我最终得到的结果是:

<environment type="float" unit="c">
  <temperature>-11.3</temperature>
</environment>

在Go Playground中的示例代码

如何格式化结构标签以将属性放在正确的元素中?

英文:

I am using the encoding/xml package in Go and the Encoder example code.

While I am able to produce workable XML, I am unable to add all of the attributes that I need.

As an example, let us use the concept of temperature reporting. What I need is something like this:

&lt;environment&gt;
  &lt;temperature type=&quot;float&quot; units=&quot;c&quot;&gt;-11.3&lt;/temperature&gt;
&lt;/environment&gt;

My struct looks like this:

type climate struct {
	XMLName		xml.Name	`xml:&quot;environment&quot;`
	Temperature	string		`xml:&quot;temperature&quot;`
    Type		string		`xml:&quot;type,attr&quot;`
    Units		string		`xml:&quot;unit,attr&quot;`
}

What I end up with looks like this:

&lt;environment type=&quot;float&quot; unit=&quot;c&quot;&gt;
  &lt;temperature&gt;-11.3&lt;/temperature&gt;
&lt;/environment&gt;

My example code in the Go Playground

How can I format the struct tags to put the attributes in the proper element?

答案1

得分: 9

你想要的XML有两个元素:&lt;environment&gt;&lt;temperature&gt;,所以你应该有两个类型(结构体)来对它们进行建模。你可以使用标签&quot;,chardata&quot;来告诉编码器将字段的值写为字符数据,而不是XML元素。

type environment struct {
    Temperature temperature `xml:"temperature"`
}

type temperature struct {
    Temperature string `xml:",chardata"`
    Type        string `xml:"type,attr"`
    Units       string `xml:"unit,attr"`
}

测试一下:

x := &environment{
    Temperature: temperature{Temperature: "-11.3", Type: "float", Units: "c"},
}

enc := xml.NewEncoder(os.Stdout)
enc.Indent("", "  ")
if err := enc.Encode(x); err != nil {
    fmt.Printf("error: %v\n", err)
}

它会产生所需的输出(在Go Playground上尝试一下):

<environment>
  <temperature type="float" unit="c">-11.3</temperature>
</environment>

请注意,如果你使用&quot;,innerxml&quot;标签,它会产生相同的结果,该标签告诉编码器直接写入原始值,而不经过常规的编组过程:

type temperature struct {
    Temperature string `xml:",innerxml"`
    Type        string `xml:"type,attr"`
    Units       string `xml:"unit,attr"`
}

输出结果是相同的。在Go Playground上尝试一下。

英文:

Your desired XML has 2 elements: &lt;environment&gt; and &lt;temperature&gt;, so you should have 2 types (structs) to model them. And you may use the tag &quot;,chardata&quot; to tell the encoder to write the field's value as character data and not as an XML element.

type environment struct {
	Temperature temperature `xml:&quot;temperature&quot;`
}

type temperature struct {
	Temperature string `xml:&quot;,chardata&quot;`
	Type        string `xml:&quot;type,attr&quot;`
	Units       string `xml:&quot;unit,attr&quot;`
}

Testing it:

x := &amp;environment{
	Temperature: temperature{Temperature: &quot;-11.3&quot;, Type: &quot;float&quot;, Units: &quot;c&quot;},
}

enc := xml.NewEncoder(os.Stdout)
enc.Indent(&quot;&quot;, &quot;  &quot;)
if err := enc.Encode(x); err != nil {
	fmt.Printf(&quot;error: %v\n&quot;, err)
}

It produces the desired output (try it on the Go Playground):

&lt;environment&gt;
  &lt;temperature type=&quot;float&quot; unit=&quot;c&quot;&gt;-11.3&lt;/temperature&gt;
&lt;/environment&gt;

Note that you get the same result if you use the &quot;,innerxml&quot; tag which tells the encoder to write the value verbatim, not subject to the usual marshaling procedure:

type temperature struct {
	Temperature string `xml:&quot;,innerxml&quot;`
	Type        string `xml:&quot;type,attr&quot;`
	Units       string `xml:&quot;unit,attr&quot;`
}

Output is the same. Try this one on the Go Playground.

huangapple
  • 本文由 发表于 2017年6月27日 03:37:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/44767424.html
匿名

发表评论

匿名网友

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

确定