Home >Backend Development >Golang >How Can I Unmarshal ISO-8859-1 Encoded XML in Go?

How Can I Unmarshal ISO-8859-1 Encoded XML in Go?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-12 19:18:11849browse

How Can I Unmarshal ISO-8859-1 Encoded XML in Go?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn