英文:
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>
如何格式化结构标签以将属性放在正确的元素中?
英文:
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:
<environment>
<temperature type="float" units="c">-11.3</temperature>
</environment>
My struct looks like this:
type climate struct {
XMLName xml.Name `xml:"environment"`
Temperature string `xml:"temperature"`
Type string `xml:"type,attr"`
Units string `xml:"unit,attr"`
}
What I end up with looks like this:
<environment type="float" unit="c">
<temperature>-11.3</temperature>
</environment>
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有两个元素:<environment>
和<temperature>
,所以你应该有两个类型(结构体)来对它们进行建模。你可以使用标签",chardata"
来告诉编码器将字段的值写为字符数据,而不是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>
请注意,如果你使用",innerxml"
标签,它会产生相同的结果,该标签告诉编码器直接写入原始值,而不经过常规的编组过程:
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: <environment>
and <temperature>
, so you should have 2 types (structs) to model them. And you may use the tag ",chardata"
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:"temperature"`
}
type temperature struct {
Temperature string `xml:",chardata"`
Type string `xml:"type,attr"`
Units string `xml:"unit,attr"`
}
Testing it:
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)
}
It produces the desired output (try it on the Go Playground):
<environment>
<temperature type="float" unit="c">-11.3</temperature>
</environment>
Note that you get the same result if you use the ",innerxml"
tag which tells the encoder to write the value verbatim, not subject to the usual marshaling procedure:
type temperature struct {
Temperature string `xml:",innerxml"`
Type string `xml:"type,attr"`
Units string `xml:"unit,attr"`
}
Output is the same. Try this one on the Go Playground.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论