我的 XML 包含预定义元素的数组,但我无法选取该数组。 XML 结构如下:
var xml_data = `<Parent>
<Val>Hello</Val>
<Children>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
</Children>
</Parent>`
这是完整的代码,这是游乐场链接 https://play.golang.org/p/VfHDqKILfA。运行此命令将获取 Parent.Val,但不会获取 Parent.Children。
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>
<Children>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
</Children>
</Parent>`
type Parent struct {
Val string
Children []Child
}
type Child struct {
Val string
}
编辑:我稍微简化了问题here https://play.golang.org/p/JkaA79p1-W。基本上我无法解组任何数组,而不仅仅是预定义结构的数组。以下是更新后的工作代码。在该示例中,只有一项最终出现在容器界面中。
func main() {
container := []Child{}
err := xml.Unmarshal([]byte(xml_data), &container)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(container)
}
/*
ONLY ONE CHILD ITEM GETS PICKED UP
*/
}
var xml_data = `
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
`
type Child struct {
Val string
}
type Parent struct {
Val string
Children []Child `xml:"Children>Child"` //Just use the '>'
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)