How can I use XML attributes as struct field?
This is my test: Each row correspond to a Person
package main
import (
"encoding/xml"
"fmt"
)
var xmlstr = `<data>
<row>
<col name='firstname'>John</col>
<col name='age'>2</col>
</row>
<row>
<col name='firstname'>3</col>
<col name='age'>4</col>
</row>
</data>`
type Data struct {
XMLName xml.Name `xml:"data"`
Person []Person `xml:"row"`
}
type Person struct {
PersonField []PersonField `xml:"col"`
}
type PersonField struct {
Name string `xml:"name,attr"`
Value string `xml:",chardata"`
}
func main() {
b := []byte(xmlstr)
var d Data
xml.Unmarshal(b, &d)
for _, person := range d.Person {
fmt.Println(person)
}
}
I go a slice of 2 struct:
{[{firstname John} {age 2}]}
{[{firstname 3} {age 4}]}
How can I get this struct instead ? Where xml attributes is use as a struct field name?
type Person struct {
Firstname string
Age int
}