1. 首页
  2. 主题
  3. Go问与答

急!Golang如何生成以下XML格式

Hanggeen · · 1549 次点击
##### 有一个需求,想生成以下的xml: ```xml <a> <b></b> <c></c> <b></b> <c></c> ... </a> ``` ##### 希望是b标签和c标签可以像数组一样不定长度插入 ##### 如果用struct转XML,里面的key不能重复,这就有点尴尬了。 ##### 麻烦各位大神教导一下,十分感谢!
试一试 ``` go package main import( &#34;fmt&#34; &#34;encoding/xml&#34; ) type A struct { XMLName xml.Name `xml:&#34;a&#34;` Values []interface{} } type B struct { XMLName xml.Name `xml:&#34;b&#34;` } type C struct { XMLName xml.Name `xml:&#34;c&#34;` } func main(){ a := A{} a.Values = []interface{}{B{},C{},B{},C{}} output, _ := xml.MarshalIndent(a, &#34; &#34;, &#34; &#34;) fmt.Println(string(output)) } ```
#1
更多评论
type A struct { XMLName xml.Name `xml:&#34;a&#34;` Bs []B `xml:&#34;b&#34; Cs []C `xml:&#34;c&#34; } type B struct{ XMLName xml.Name `xml:&#34;b&#34;` } type C struct{ XMLName xml.Name `xml:&#34;c&#34;` } ... paramStream, err := xml.Marshal(a) ...
#2
```go type A struct { XMLName xml.Name xml:&#34;a&#34; Bs []B xml:&amp;#34;b&amp;#34; Cs []Cxml:&#34;c&#34; } type B struct{ XMLName xml.Name xml:&#34;b&#34; } type C struct{ XMLName xml.Name xml:&#34;c&#34; } ... paramStream, err := xml.Marshal(a) ... ```
#3

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中