问题
当XML文件中有一些特殊的UTF-8字符,并且您的SAX解析器未配置为正确解析UTF-8时,将引发以下异常。
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:
Invalid byte 1 of 1-byte UTF-8 sequence.
...
解
解决方案非常简单,以UTF-8格式获取内容,并覆盖SAX输入源。
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");
saxParser.parse(is, handler);
您可以在此处阅读完整的示例–