Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence. when using Hebrew chars

I'm trying to parse XML file which contains Hebrew chars. I know that the file is correct because if I output the file (from a different software) without the hebrew chars, it parses just fine.

I tried many things, but I always get this error

MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.

My latest attempt was to open it using FileInputStream and specify the encoding

DocumentBuilder db = dbf.newDocumentBuilder();
document = db.parse(new FileInputStream(new File(xmlFileName)), "Cp1252");

(Cp1252 is an encoding that worked for me in a different app) But I got the same result.

Tried using ByteArray as well, nothing worked.

Any suggestions?

like image 681
La bla bla Avatar asked Dec 14 '12 14:12

La bla bla


2 Answers

if you know the correct encoding of the file and it's not "utf-8", then you can either add it to the xml header:

<?xml version="1.0" encoding="[correct encoding here]" ?>

or parse it as a Reader:

db.parse(new InputStreamReader(new FileInputStream(new File(xmlFileName)), "[correct encoding here]"));
like image 63
jtahlborn Avatar answered Sep 19 '22 10:09

jtahlborn


The solution is quite simple, get the content in UTF-8 format, and override the SAX input source.

File file = new File("c:\\file-utf.xml");
InputStream inputStream= new FileInputStream(file);
Reader reader = new InputStreamReader(inputStream,"UTF-8");

InputSource is = new InputSource(reader);
// is.setEncoding("UTF-8"); -> This line causes error! Content is not allowed in prolog

saxParser.parse(is, handler);

You can read the full example here – http://www.mkyong.com/java/how-to-read-utf-8-xml-file-in-java-sax-parser/

like image 29
Raaam Avatar answered Sep 19 '22 10:09

Raaam