saxReader.read()报org.xml.sax.SAXParseException: Invalid byte 2 of 2-byte UTF-8 sequence.

saxReader.read()读取的流中包含中文报错:
org.dom4j.DocumentException: Error on line 1 of document    : Invalid byte 2 of 2- byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2- byte UTF-8 sequence.
  at org.dom4j.io.SAXReader.read(SAXReader.java:482)
  at org.dom4j.io.SAXReader.read(SAXReader.java:343)
  at com.gemship.edi.parse.EdiCommonUtils.loadXML(EdiCommonUtils.java:535)
  at TestEdiCommonUtils.testLoadXML(TestEdiCommonUtils.java:39)
  at TestEdiCommonUtils.main(TestEdiCommonUtils.java:48)
Nested exception:    
org.xml.sax.SAXParseException: Invalid byte 2 of 2- byte UTF-8 sequence.
  at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
  at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:215)
  at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:386)
  at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1810)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
  at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
  at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
  at org.dom4j.io.SAXReader.read(SAXReader.java:465)
  at org.dom4j.io.SAXReader.read(SAXReader.java:343)
  at com.gemship.edi.parse.EdiCommonUtils.loadXML(EdiCommonUtils.java:535)
  at TestEdiCommonUtils.testLoadXML(TestEdiCommonUtils.java:39)
  at TestEdiCommonUtils.main(TestEdiCommonUtils.java:48)
 
解决如下:
SAXReader saxReader = new SAXReader();
byte[] bytes = requestMsg.getBytes();
InputStream in = new ByteArrayInputStream(bytes);
InputStreamReader strInStream = new InputStreamReader(in, "GBK");
Document document = saxReader.read(strInStream);
 

你可能感兴趣的:(xml,dom4j,中文,SAXReader,休闲)