I don't know how to make this code work. I'm just trying to parse a simple XML file like this:
package main
import (
"encoding/xml"
"fmt"
)
type Data struct {
XMLName xml.Name `xml:"data"`
Nam string `xml:"nam,attr"`
}
type Struct struct {
XMLName xml.Name `xml:"struct"`
Data []Data
}
func main() {
x := `
<struct>
<data nam="MESSAGE_TYPE">
</data>
<data nam="MESSAGE_TYPE2">
</data>
</struct>
`
s := Struct{}
err := xml.Unmarshal([]byte(x), &s)
if err != nil {
panic(err)
}
fmt.Printf("%v
", s)
fmt.Println(s.Data)
}
what I got is:
{{ struct} []}
[]
But when I change the "data" elements to uppercase, like this:
package main
import (
"encoding/xml"
"fmt"
)
type Data struct {
XMLName xml.Name `xml:"Data"`
Nam string `xml:"nam,attr"`
}
type Struct struct {
XMLName xml.Name `xml:"struct"`
Data []Data
}
func main() {
x := `
<struct>
<Data nam="MESSAGE_TYPE">
</Data>
<Data nam="MESSAGE_TYPE2">
</Data>
</struct>
`
s := Struct{}
err := xml.Unmarshal([]byte(x), &s)
if err != nil {
panic(err)
}
fmt.Printf("%v
", s)
fmt.Println(s.Data)
}
I got this:
{{ struct} [{{ Data} MESSAGE_TYPE} {{ Data} MESSAGE_TYPE2}]}
[{{ Data} MESSAGE_TYPE} {{ Data} MESSAGE_TYPE2}]
Can anybody tell me why?