0

I'm trying to put a java exception stack trace as a text element in an XML file. A stack trace has line breaks and a tab character in the beginning of most lines, so to display it properly, I need to show whitespace which I do by putting white-space: pre in the XSL file. However, in the XML file I get both the white space which belongs to the stack trace and extra indentation white space. This is what ends up in the XML file:

                <Comment>java.lang.Exception: foo
            at ...
            at ...
            at ...
        Caused by: java.lang.IllegalArgumentException: fee
            at ...
        </Comment>

All lines except the first start with 8 extra spaces when I display the XML (the last 4 spaces of the 'at' lines are the tab character). Can I change how the XML is generated or displayed to show the stack trace properly?

Code excerpts:

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbfac.newDocumentBuilder();
Document doc = db.newDocument();
...
Element comment = doc.createElement("Comment");
comment.setTextContent(stacktrace);

XSL excerpt (the "Description" here is the same element as the "Comment" above):

                                <div class="GROUP">
                                    <table class="GROUP"
                                        style="width: 100%; table-layout: auto; white-space: pre">
                                        <tr>
                                            <td class="RESULT">Id</td>
                                            <td class="RESULT">Result</td>
                                            <td class="RESULT">Description</td>
H.v.M.
  • 1,348
  • 3
  • 16
  • 42
  • 1
    Either add an `xml:space="preserve"` attribute to the element that holds the text or put your text into CDATA nodes (e.g. `<![CDATA[Text goes here]]>`). – Thomas Behr Aug 24 '23 at 14:35
  • You might need to create your own solution. If you run the generated XML string thru a "pretty print" function, your text nodes will most likely normalize. What have you tried? – hfontanez Aug 24 '23 at 14:36
  • I think I misinterpreted the question at first - your problem is not the displaying of the whitespace, but that extra whitespace is added by either your XML generator or some pretty printing function? How exactly do you apply the XSL, using xslt? – l4mpi Aug 24 '23 at 14:42
  • @ThomasBehr Neither solution seemed to do anything. In both cases, if I also included `white-space: pre` all white space was shown, and otherwise none was shown. – H.v.M. Aug 24 '23 at 15:09
  • @l4mpi I don't know. There's a report.xml, a report.xsl and a style.css. I open the xml in firefox. Does that answer your question? – H.v.M. Aug 24 '23 at 15:17
  • @H.v.M. You wrote: "However, in the XML file I get both the white space which belongs to the stack trace and extra indentation white space." and "All lines except the first start with 8 extra spaces when I display the XML." If you *manually* remove the 8 spaces of extra indentation white space from your XML file, does your stacktrace display correctly? – Thomas Behr Aug 24 '23 at 16:05
  • @ThomasBehr Yes. – H.v.M. Aug 24 '23 at 16:29
  • @H.v.M. So, to solve your problem you need to change how your XML is generated to prevent the XML writer to add those extra indentation white spaces. How that can be achieved depends on how you write your XML. I don't mean the code to build the XML document, but the code to write the document to a file. And which version of Java do you use for that? – Thomas Behr Aug 24 '23 at 17:08
  • @ThomasBehr I'm on Java 17. – H.v.M. Aug 24 '23 at 17:50
  • @H.v.M. Ok, fine. And how do you write that XML file? I don't know if it is still possible to edit your original question, but if it is, do that. Much easier to read formatted code. – Thomas Behr Aug 24 '23 at 18:23
  • @ThomasBehr Thanks mate, you put me on the right trail. Turns out there was manual indentation going on in order to merge two files. Mystery solved. Idiot jannies who lock questions without reading them and then don't review can have their way. :) – H.v.M. Aug 25 '23 at 17:27

0 Answers0