【发布时间】:2019-05-15 10:41:19
【问题描述】:
根据下面的链接,我们可以使用>或其他结构从嵌套的xml中获取数据。
How do I unmarshal nested XML elements into an array?
但是如果不使用这样的结束标签,它就不起作用了。
代码:
package main
import (
"fmt"
"encoding/xml"
)
func main() {
container := Parent{}
err := xml.Unmarshal([]byte(xml_data), &container)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(container)
}
}
var xml_data = `<Parent>
<Val>Hello</Val>
<Child Val="Hello"/>
<Child Val="Hello"/>
<Child Val="Hello"/>
</Parent>`
type Parent struct {
Val string
Children Children
}
type Children struct {
Child []Child
}
type Child struct {
Val string
}
结果:
{Hello {[]}}
有什么办法吗?
【问题讨论】:
标签: xml go struct slice unmarshalling