My XML contains an array of predefined elements, but I can't pick up the array. Here is the XML structure:
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>`
Here is the full code and here is the playground link. Running this will pick up Parent.Val, but not 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
}
EDIT: I simplified the problem a bit here. Basically I can't unmarshal any array, not just of predefined structure. Below is the updated work code. In that example, only one item ends up in the container interface.
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 '>'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With