2 回答

TA貢獻1852條經驗 獲得超7個贊
type Parent struct {
Val string
Children []Child `xml:"Children>Child"` //Just use the '>'
}

TA貢獻1719條經驗 獲得超6個贊
對于這種嵌套,您還需要一個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 Children
}
type Children struct {
Child []Child
}
type Child struct {
Val string
}
也貼在這里:去游樂場
請注意,您的代碼可以使用這種 XML 結構(在將變量名從 更改Children為 之后Child):
<Parent>
<Val>Hello</Val>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
<Child><Val>Hello</Val></Child>
</Parent>
- 2 回答
- 0 關注
- 273 瀏覽
添加回答
舉報