Assume the following xml element, with an attribute and a floating point value:
1.23
4.56
&l
Below you can find the code to unmarshal the given data.
xml.Name
field in structure as long as it is not ambiguous which structure should be used.package main
import (
"encoding/xml"
"fmt"
)
type Root struct {
Things []Thing `xml:"thing"`
}
type Thing struct {
Prop int `xml:"prop,attr"`
Value float64 `xml:",chardata"`
}
func main() {
data := `
<root>
<thing prop="1">1.23</thing>
<thing prop="2">4.56</thing>
</root>
`
thing := &Root{}
err := xml.Unmarshal([]byte(data), thing)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(thing)
}