我在转换文档后得到了这个返回XML。以下是XML返回示例:
<ERROR>
<DETAILS><![CDATA[SomeKindOfException: Failed]]> <![CDATA[
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)]]> <![CDATA[
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)]]> <![CDATA[
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:136)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:97)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:960)]]> <![CDATA[
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1064)]]> <![CDATA[
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3826)]]> <![CDATA[
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)]]> <![CDATA[
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:976)]]> <![CDATA[
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)]]> <![CDATA[
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)]]> <![CDATA[
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:459)]]> <![CDATA[
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:526)]]> <![CDATA[
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:312)]]> <![CDATA[
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:283)]]> <![CDATA[
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)]]> <![CDATA[
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)]]> <![CDATA[
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)]]> <![CDATA[
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)]]> <![CDATA[
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)]]> <![CDATA[
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)]]> <![CDATA[
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)]]> <![CDATA[
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)]]> <![CDATA[
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)]]> <![CDATA[
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1862)]]> <![CDATA[
]]></DETAILS>
</ERROR>
这就是我进行转换的方式:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.newDocument();
Element errorNode = document.createElement("ERROR");
Element detailNode = document.createElement("DETAILS");
CDATASection cd = document.createCDATASection(stackTraceDetails);
detailNode.appendChild(cd);
errorNode.appendChild(detailNode);
document.appendChild(errorNode);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
Writer stringWriter = new StringWriter();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "DETAILS");
transformer.transform(new DOMSource(document.getDocumentElement()), new StreamResult(stringWriter));
return stringWriter.toString();
我无法弄清楚这是怎么回事。任何机构都知道这是怎么回事?
答案 0 :(得分:1)
异常堆栈跟踪包含行结尾\r\n
(Windows)。然而,\n
是本地行结束(Linux?),而\r
表示为数字XML实体
。这只能在<![CDATA[...]]>
部分之外表示。
CDATASection cd = document.createCDATASection(stackTraceDetails.replace("\r", ""));
变化可以想象。