I am developing an app for Android where I have to parse different XML files. Most of them are encoded in UTF-8, but a few may be encoded in ISO-8859-1.
HttpURLConnection con = (HttpURLConnection) url.openConnection();
...
in = con.getInputStream();
InputSource is = new InputSource(in);
...
parser.parse(is, handler);
My code for handling the input looks like above. The java documentation says about the InputSource:
If there is no character stream, but there is a byte stream, the parser will use that byte stream, using the encoding specified in the InputSource or else (if no encoding is specified) autodetecting the character encoding using an algorithm such as the one in the XML specification.
I am passing in a ByteStream and I am don't specify an encoding, so according to the documentation the encoding should be auto detected. But it doesn't. All files that are encoded in UTF-8 are fine, but the ISO-8859-1 ones are not (I am getting a Parser Expat... Exception for some invalid characters). If I set the encoding of the InputSource manually to "ISO-8859-1" it behaves the other way round.
How can I solve this? I searched Google and Stackoverflow for hours, but not finding a solution. I also tried to pass a CharacterStream to the InputSource, but some characters (äöüÄÖÜß) in the ISO-8859-1 files are still displayed as "?" in my app.
Thanks in advance!