I'm trying to unmarshal an XML feed containing German characters (e.g. ß, ä, Ö, ü, etc) into a struct, which results in the error: xml: encoding "utf-16" declared but Decoder.CharsetReader is nil unmarshal successful
Basically this is what I'm doing (omitted error checking for the parts that work):
resp, _ := http.Get(url)
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
err = xml.Unmarshal(bodyBytes, &target)
if err != nil {
fmt.Println(err)
}
I've tried to convert xml to json using github.com/basgys/goxml2json
, I've tried to convert to string and back to []byte before unmarshal, I've tried to use various decoders posted in other SO answers (since it say charset reader is nil), such as:
reader := bytes.NewReader(bodyBytes)
decoder := xml.NewDecoder(reader)
decoder.CharsetReader = charset.NewReader
err = decoder.Decode(&target)
if err != nil {
fmt.Println(err)
}
No matter what I've tried it fails to unmarshal/decode the xml feed into the struct.. in some cases it ends up converting all the text to Chinese rather than German.