How to fix Invalid byte 1 of 1-byte UTF-8 sequence

Solution 1:

How to fix this issue ?

Read the data using the correct character encoding. The error message means that you are trying to read the data as UTF-8 (either deliberately or because that is the default encoding for an XML file that does not specify <?xml version="1.0" encoding="somethingelse"?>) but it is actually in a different encoding such as ISO-8859-1 or Windows-1252.

To be able to advise on how you should do this I'd have to see the code you're currently using to read the XML.

Solution 2:

  1. Open the xml in notepad
  2. Make sure you dont have extra space at the beginning and end of the document.
  3. Select File -> Save As
  4. select save as type -> All files
  5. Enter file name as abcd.xml
  6. select Encoding - UTF-8 -> Click Save

Solution 3:

Try:

InputStream inputStream= // Your InputStream from your database.
Reader reader = new InputStreamReader(inputStream,"UTF-8");

InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");

saxParser.parse(is, handler);

If it's anything else than UTF-8, just change the encoding part for the good one.

Solution 4:

I was getting the xml as a String and using xml.getBytes() and getting this error. Changing to xml.getBytes(Charset.forName("UTF-8")) worked for me.