英文:
marshal an array to a single xml element in go
问题
我正在通过编写一个生成Collada格式文件的程序来学习Go语言,该格式使用XML描述几何形状。
你可以使用注释来标记你的结构体,几乎一切都按照你的期望工作,除了我无法弄清楚如何将数组编组为一个XML元素 - 我总是最终生成N个元素。
换句话说,我希望得到:
<input>
<p>0 1 2</p>
</input>
而不是:
<input>
<p>0</p>
<p>1</p>
<p>2</p>
</input>
代码如下:
package main
import (
"encoding/xml"
"os"
)
func main() {
type Vert struct {
XMLName xml.Name `xml:"input"`
Indices []int `xml:"p"`
}
v := &Vert{Indices:[]int{0, 1, 2}}
output, err := xml.MarshalIndent(v, "", " ")
if err == nil {
os.Stdout.Write(output)
}
}
来自encoding/xml/marshal.go的各种注释(和代码)似乎暗示我没有办法:
> // Marshal通过编组每个元素来处理数组或切片。
> // 切片和数组遍历元素。它们没有封闭标签。
奇怪的是,如果我将数组类型更改为uint8,数组根本不会被编组。
如果我没有办法,我可能会使用xml:",innerxml"注释来自己替换数组。
英文:
I'm learning Go by writing a program that generates files in Collada format, which describes geometry using XML.
You annotate your structs and almost everything works as you'd expect, except I can't figure out how to marshal arrays into one XML element - I always end up generating N elements.
In other words, I'd like
<input>
<p>0 1 2</p>
</input>
instead of
<input>
<p>0</p>
<p>1</p>
<p>2</p>
</input>
The code is as follows
package main
import (
"encoding/xml"
"os"
)
func main() {
type Vert struct {
XMLName xml.Name `xml:"input"`
Indices []int `xml:"p"`
}
v := &Vert{Indices:[]int{0, 1, 2}}
output, err := xml.MarshalIndent(v, "", " ")
if err == nil {
os.Stdout.Write(output)
}
}
Various comments (and code) from encoding/xml/marshal.go seem to imply that I'm out of luck:
> // Marshal handles an array or slice by marshalling each of the elements.
> // Slices and arrays iterate over the elements. They do not have an enclosing tag.
Strangely, if I change my array type to uint8, the array is not marshalled at all.
If I am out of luck, I'll probably use the xml:",innerxml" annotation to substitute the array myself.
答案1
得分: 1
正如你猜到的那样,encoding/xml无法直接完成这个任务。你可以尝试以下方法:
import (
"strconv"
"strings"
)
type Vert struct {
P string `xml:"p"`
}
func (v *Vert) SetIndices(indices []int) {
s := make([]string, len(indices))
for i := range indices {
s[i] = strconv.FormatInt(int64(indices[i]), 10)
}
v.P = strings.Join(s, " ")
}
**编辑:**我最初写的是一个getter而不是setter。
英文:
As you guessed, encoding/xml will not be able to do this out-of-the-box. You can do something like this instead:
import (
"strconv"
"strings"
)
type Vert struct {
P string `xml:"p"`
}
func (v *Vert) SetIndices(indices []int) {
s := make([]string, len(indices))
for i := range indices {
s[i] = strconv.FormatInt(int64(indices[i]), 10)
}
v.P = strings.Join(s, " ")
}
EDIT: I originally wrote a getter instead of setter.
答案2
得分: 0
在最后,我为这种类型的数组做了自己的xml编组,通过将类型更改为string
并使用innerxml
属性。
package main
import (
"encoding/xml"
"os"
"fmt"
"bytes"
)
func MyMarshalArray(indices []int) string {
var buffer bytes.Buffer
buffer.WriteString("<p>")
for i := 0; i < len(indices); i++ {
buffer.WriteString(fmt.Sprintf("%v ", indices[i]))
}
buffer.WriteString("</p>")
return buffer.String()
}
func main() {
type Vert struct {
XMLName xml.Name `xml:"input"`
Indices string `xml:",innerxml"`
}
v := &Vert{Indices:MyMarshalArray([]int{0, 1, 2})}
output, err := xml.MarshalIndent(v, "", " ")
if err == nil {
os.Stdout.Write(output)
}
}
英文:
In the end I did my own xml marshalling for this type of array,
by changing the type to string
and using the innerxml
attribute.
package main
import (
"encoding/xml"
"os"
"fmt"
"bytes"
)
func MyMarshalArray(indices []int) string {
var buffer bytes.Buffer
buffer.WriteString("<p>")
for i := 0; i < len(indices); i++ {
buffer.WriteString(fmt.Sprintf("%v ", indices[i]))
}
buffer.WriteString("</p>")
return buffer.String()
}
func main() {
type Vert struct {
XMLName xml.Name `xml:"input"`
Indices string `xml:",innerxml"`
}
v := &Vert{Indices:MyMarshalArray([]int{0, 1, 2})}
output, err := xml.MarshalIndent(v, "", " ")
if err == nil {
os.Stdout.Write(output)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论