Search code examples
utf-8character-encodinggo

Unmarshal an ISO-8859-1 XML input in Go


When your XML input isn't encoded in UTF-8, the Unmarshal function of the xml package seems to require a CharsetReader.

Where do you find such a thing ?


Solution

  • Updated answer for 2015 & beyond:

    import (
        "encoding/xml"
        "golang.org/x/net/html/charset"
    )
    reader := bytes.NewReader(theXml)
    decoder := xml.NewDecoder(reader)
    decoder.CharsetReader = charset.NewReaderLabel
    err = decoder.Decode(&parsed)