2 回答

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊
type Parent struct {
Val string
Children []Child `xml:"Children>Child"` //Just use the '>'
}

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
對(duì)于這種嵌套,您還需要一個(gè)Children元素結(jié)構(gòu):
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 Children
}
type Children struct {
Child []Child
}
type Child struct {
Val string
}
也貼在這里:去游樂(lè)場(chǎng)
請(qǐng)注意,您的代碼可以使用這種 XML 結(jié)構(gòu)(在將變量名從 更改Children為 之后Child):
<Parent>
<Val>Hello</Val>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
</Parent>
- 2 回答
- 0 關(guān)注
- 266 瀏覽
添加回答
舉報(bào)