I must be missing something really obvious... Am trying to parse a simple XML file, following the example from ExampleUnmarshal()
in here: http://golang.org/src/pkg/encoding/xml/example_test.go
As you'll see at the bottom of this, none of the attributes or child elements are being mapped - either direction - Marshal or Unmarshal. From what I can tell this is almost the exact same thing they are doing in example_test.go above (the only differences I can see are the that types in that test are within the scope of the function - which I tried, makes no diff, and they are using child elements and not attributes - except for id
- but per doc name,attr should work afaict).
Code looks like this:
package main
import (
"fmt"
"encoding/xml"
"io/ioutil"
)
type String struct {
XMLName xml.Name `xml:"STRING"`
lang string `xml:"lang,attr"`
value string `xml:"value,attr"`
}
type Entry struct {
XMLName xml.Name `xml:"ENTRY"`
id string `xml:"id,attr"`
strings []String
}
type Dictionary struct {
XMLName xml.Name `xml:"DICTIONARY"`
thetype string `xml:"type,attr"`
ignore string `xml:"ignore,attr"`
entries []Entry
}
func main() {
dict := Dictionary{}
b := []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DICTIONARY type="multilanguage" ignore="en">
<ENTRY id="ActionText.Description.AI_ConfigureChainer">
<STRING lang="en" value="ActionText.Description.AI_ConfigureChainer"/>
<STRING lang="da" value=""/>
<STRING lang="nl" value=""/>
<STRING lang="fi" value=""/>
</ENTRY>
</DICTIONARY>
`)
err := xml.Unmarshal(b, &dict)
if err != nil { panic(err) }
fmt.Println(dict) // prints: {{ DICTIONARY} []}
dict.ignore = "test"
out, err := xml.MarshalIndent(&dict, " ", " ")
fmt.Println(string(out)) // prints: <DICTIONARY></DICTIONARY>
// huh?
}