将一个数组编组为一个单独的 XML 元素在 Go 中。

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

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

&lt;input&gt;
    &lt;p&gt;0 1 2&lt;/p&gt;
&lt;/input&gt; 

instead of

&lt;input&gt;
    &lt;p&gt;0&lt;/p&gt;
    &lt;p&gt;1&lt;/p&gt;
    &lt;p&gt;2&lt;/p&gt;
&lt;/input&gt; 

The code is as follows

package main

import (
    &quot;encoding/xml&quot;
    &quot;os&quot;
)

func main() {
    type Vert struct {
        XMLName xml.Name    `xml:&quot;input&quot;`
        Indices     []int   `xml:&quot;p&quot;`
    }

    v := &amp;Vert{Indices:[]int{0, 1, 2}}
    output, err := xml.MarshalIndent(v, &quot;&quot;, &quot;    &quot;)
    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 (
	&quot;strconv&quot;
	&quot;strings&quot;
)

type Vert struct {
	P string `xml:&quot;p&quot;`
}

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, &quot; &quot;)
}

**编辑:**我最初写的是一个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 (
	&quot;strconv&quot;
	&quot;strings&quot;
)

type Vert struct {
	P string `xml:&quot;p&quot;`
}

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, &quot; &quot;)
}

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 (
    &quot;encoding/xml&quot;
    &quot;os&quot;
    &quot;fmt&quot;
    &quot;bytes&quot;
)

func MyMarshalArray(indices []int) string {
    var buffer bytes.Buffer

    buffer.WriteString(&quot;&lt;p&gt;&quot;)
    for i := 0; i &lt; len(indices); i++ {
        buffer.WriteString(fmt.Sprintf(&quot;%v &quot;, indices[i]))
    }
    buffer.WriteString(&quot;&lt;/p&gt;&quot;)
    return buffer.String()
}

func main() {
    type Vert struct {
        XMLName xml.Name    `xml:&quot;input&quot;`
        Indices     string  `xml:&quot;,innerxml&quot;`
    }

    v := &amp;Vert{Indices:MyMarshalArray([]int{0, 1, 2})}
    output, err := xml.MarshalIndent(v, &quot;&quot;, &quot;    &quot;)
    if err == nil {
        os.Stdout.Write(output)
    }
}

huangapple
  • 本文由 发表于 2012年12月10日 09:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/13793698.html
匿名

发表评论

匿名网友

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

确定