Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse file with special characters #118

Open
jiwanovski87 opened this issue Nov 4, 2024 · 1 comment
Open

Parse file with special characters #118

jiwanovski87 opened this issue Nov 4, 2024 · 1 comment

Comments

@jiwanovski87
Copy link

Hey everyone,

currently I am trying to parse an xml file with a country specific character.
As example: In Germany there is the character ä. In XML this is represented as ä

So when I just do the following code I receive this error message. How can I fix this?
encoding/xml.SyntaxError {Msg: "invalid character entity ä", Line: 7}

xf, err := os.Open(f)
if err != nil {
	p.Logger.Errorf("Cannot open file: %s", f)
}
defer xf.Close()

doc, err := xmlquery.Parse(xf)
if err != nil {
	fmt.Println(err)
}
@jiwanovski87
Copy link
Author

Ok, I believe I have fixed it with the following code:

doc, err := xmlquery.ParseWithOptions(xf, xmlquery.ParserOptions{
	Decoder: &xmlquery.DecoderOptions{
		CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
			utf8_reader := charmap.ISO8859_1.NewDecoder().Reader(input)
			return utf8_reader, nil
		},
	},
})
if err != nil {
	fmt.Println(err)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant