]]&gt;&#13; <! - [CDATA [散布在已转换的xml文档

时间:2016-09-14 03:10:35

标签: java xml

我在转换文档后得到了这个返回XML。以下是XML返回示例:

 <ERROR>
<DETAILS><![CDATA[SomeKindOfException: Failed]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:136)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:97)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:960)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1064)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3826)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:976)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)]]>&#13;<![CDATA[
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)]]>&#13;<![CDATA[
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:459)]]>&#13;<![CDATA[
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:526)]]>&#13;<![CDATA[
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:312)]]>&#13;<![CDATA[
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:283)]]>&#13;<![CDATA[
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)]]>&#13;<![CDATA[
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)]]>&#13;<![CDATA[
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)]]>&#13;<![CDATA[
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)]]>&#13;<![CDATA[
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)]]>&#13;<![CDATA[
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)]]>&#13;<![CDATA[
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)]]>&#13;<![CDATA[
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)]]>&#13;<![CDATA[
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)]]>&#13;<![CDATA[
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1862)]]>&#13;<![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();

我无法弄清楚这是怎么回事。任何机构都知道这是怎么回事?

1 个答案:

答案 0 :(得分:1)

异常堆栈跟踪包含行结尾\r\n(Windows)。然而,\n是本地行结束(Linux?),而\r表示为数字XML实体&#13;。这只能在<![CDATA[...]]>部分之外表示。

CDATASection cd = document.createCDATASection(stackTraceDetails.replace("\r", ""));

变化可以想象。