怎么解决? com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:3字节UTF-8序列的无效字节3

时间:2015-11-02 06:37:52

标签: java xml exception utf-8 malformed

我在我的代码中使用了DocumentBuilder类的parse()方法。

  
      
  • 文件doc = dBuilder.parse(bis);
  •   

在上面一行,它抛出异常:

com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.
       at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)
       at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)
       at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
       at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(Unknown Source)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
       at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
       at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
       at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
       at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
       at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
       at org.econ.alm.client.AbstractALMClient.getEntitiesBMD(AbstractALMClient.java:226)
       at org.econ.alm.extracter.MDBTestExtractor.run(MDBTestExtractor.java:204)
       at java.util.TimerThread.mainLoop(Unknown Source)
       at java.util.TimerThread.run(Unknown Source)

如果有人知道根本原因和解决方案,请建议。

提前致谢。

1 个答案:

答案 0 :(得分:0)

对我来说,更改解析功能是:从文件XML到InputSource(将字符集更改为UTF)

 InputStream inputStream= new FileInputStream(file); 
 Reader reader = new InputStreamReader(inputStream,"UTF-8");        
 InputSource is = new InputSource(reader); 
 is.setEncoding("UTF-8");
 doc = dBuilder.parse(is);