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.
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.
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)
}
}