Unmarshaling XML Encoded in ISO-8859-1 Using Go
The popular Unmarshal function in Go's xml package can be challenging if your XML input is not encoded in UTF-8. To handle such inputs, Go requires a CharsetReader.
Where to Find a CharsetReader?
In recent versions of Go (2015 onwards), the golang.org/x/net/html/charset package provides the necessary functionality. The NewReaderLabel function within this package can perform the necessary conversion.
Code Example
The following code snippet demonstrates how to use NewReaderLabel to correctly unmarshal an XML input encoded in ISO-8859-1:
import ( "encoding/xml" "golang.org/x/net/html/charset" "bytes" ) var theXml = [...]byte{byte(0x3C), byte(0x3F), byte(0x78), byte(0x6D), /* ... */} reader := bytes.NewReader(theXml) decoder := xml.NewDecoder(reader) decoder.CharsetReader = charset.NewReaderLabel err := decoder.Decode(&parsed)
By incorporating this code, Go can successfully unmarshal XML inputs encoded in ISO-8859-1.
The above is the detailed content of How Can I Unmarshal ISO-8859-1 Encoded XML in Go?. For more information, please follow other related articles on the PHP Chinese website!