xml中有特殊字元,導致解析時出錯

杏少發表於2015-01-29
2015-01-29 00:10:22,075  ERROR commonapi.CommonApiAction - errorCode:5000,5000-00;Description:程式異常。Error on line 1 of document  : An invalid XML character (Unicode: 0x19) was found in the CDATA section. Nested exception: An invalid XML character (Unicode: 0x19) was found in the CDATA section.
org.dom4j.DocumentException: Error on line 1 of document  : An invalid XML character (Unicode: 0x19) was found in the CDATA section. Nested exception: An invalid XML character (Unicode: 0x19) was found in the CDATA section.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.dom4j.DocumentHelper.parseText(DocumentHelper.java:278)
at com.hoodong.engine.commonapi.CommonApiAction.getWapDocsSearchJsonInfo(CommonApiAction.java:1866)
at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:114)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)
at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)
at com.caucho.server.webapp.DispatchFilterChain.doFilter(DispatchFilterChain.java:97)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:265)
at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:293)
at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:111)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:114)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)
at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)
at com.hudong.keel.web.filter.CookieAutoLogonFilter.doFilterLogic(CookieAutoLogonFilter.java:137)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hudong.keel.web.filter.ProductContextFilter.doFilterLogic(ProductContextFilter.java:167)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hudong.keel.web.filter.HttpSessionFilter.doFilterLogic(HttpSessionFilter.java:95)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hoodong.engine.filter.CaptchaFilter.doFilterLogic(CaptchaFilter.java:68)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hudong.keel.web.filter.PerformanceMonitorFilter.doFilterLogic(PerformanceMonitorFilter.java:61)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hoodong.engine.common.RecordSession.doFilter(RecordSession.java:94)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hoodong.engine.common.EncodeServlet.doFilter(EncodeServlet.java:41)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:187)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:265)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:273)
at com.caucho.server.port.TcpConnection.run(TcpConnection.java:682)
at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:743)
at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:662)
at java.lang.Thread.run(Thread.java:619)
Nested exception: 
org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x19) was found in the CDATA section.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanCDATASection(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.dom4j.DocumentHelper.parseText(DocumentHelper.java:278)
at com.hoodong.engine.commonapi.CommonApiAction.getWapDocsSearchJsonInfo(CommonApiAction.java:1866)
at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:114)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)
at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)
at com.caucho.server.webapp.DispatchFilterChain.doFilter(DispatchFilterChain.java:97)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:265)
at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:293)
at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:111)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:114)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:91)
at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)
at com.hudong.keel.web.filter.CookieAutoLogonFilter.doFilterLogic(CookieAutoLogonFilter.java:137)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hudong.keel.web.filter.ProductContextFilter.doFilterLogic(ProductContextFilter.java:167)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hudong.keel.web.filter.HttpSessionFilter.doFilterLogic(HttpSessionFilter.java:95)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hoodong.engine.filter.CaptchaFilter.doFilterLogic(CaptchaFilter.java:68)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hudong.keel.web.filter.PerformanceMonitorFilter.doFilterLogic(PerformanceMonitorFilter.java:61)
at com.hudong.keel.web.filter.AbstractFilter.doFilter(AbstractFilter.java:111)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hoodong.engine.common.RecordSession.doFilter(RecordSession.java:94)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.hoodong.engine.common.EncodeServlet.doFilter(EncodeServlet.java:41)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:187)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:265)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:273)
at com.caucho.server.port.TcpConnection.run(TcpConnection.java:682)
at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:743)
at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:662)

at java.lang.Thread.run(Thread.java:619)


在網上找了找原因和解決辦法,現在貼到這裡:


解析XML檔案時出現非法字元的Exception(即使該字元位於CDATA段內):    org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0xb) was found in the CDATA section.
原因在於:根據W3C標準,有一些字元不能出現在XML檔案中:
    0x00 - 0x08
    0x0b - 0x0c
    0x0e - 0x1f
解析XML時遇到這些字元就會出錯

解決方法:
對有可能出問題的XML檔案,進行字元過濾後再進行解析。


public static String stripNonValidXMLChars(String str) {
  
if (str == null || "".equals(str)) {
    
return str;
  }
  
return str.replaceAll("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]""");
}

相關文章