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?
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]"));
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With