<span style="color:#003300;">今天在解析XML出问题了,以前代码是没有问题的. 发现是客户接口那边多了些原来没有的字符导致的. </span>
<span style="color:#ff0000;">com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.</span>
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:687)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:372)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1753)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanLiteral(XMLEntityScanner.java:1074)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanAttributeValue(XMLScanner.java:948)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanAttribute(XMLDocumentFragmentScannerImpl.java:1507)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1280)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2717)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
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:123)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:237)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:300)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
at javacommon.util.DomXmlParseUtils.getDocument(DomXmlParseUtils.java:53)
at javacommon.util.DomXmlParseUtils.getNode(DomXmlParseUtils.java:63)
at com.buyantech.qb.controller.QBController.convertRichText2SimpleText(QBController.java:444)
at com.buyantech.qb.controller.QBController.remoteChildSystemInfoList(QBController.java:391)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
导致的原因可能是:
1.如果是读取外部配置文件出现这个错误,可能是xml 声明为UTF-8格式,保存用的别的格式. 这样咱们在处理的时候,怎么看xml里面的格式都是对的,实际是文件保存的格式不对. 解决办法:另存为一致格式.
2.调用接口获取到的XML文档字符串,我们在将其转换成Java中Document时,调用xmlString.getBytes(); 这里用的平台的默认字符集,如果有字符不支持也会报错.
解决办法如下:
原异常代码:
ByteArrayInputStream bis = new ByteArrayInputStream(xmlContent.getBytes());
return DOCUMENT_BUILDER.parse(bis);
ByteArrayInputStream bis = new ByteArrayInputStream(<span style="color:#006600;">xmlContent.getBytes("UTF-8")</span>);
return DOCUMENT_BUILDER.parse(bis);
记录下.