Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control line endings that javax.xml.transform.Transformer creates?

Tags:

java

dom

xml

I am transforming a DOM document (org.w3c.dom.Document) to a Stream using

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name());
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult output = new StreamResult(out);
Source input = new DOMSource(document);
transformer.transform(input, output);

The document contains text nodes with linefeeds ('\n'). In the output they are replaced with CRLF ("\r\n"), which is not desired. Is there a way to control this (besides replacing them afterwards, of course)?

I have no control over the documents DTD (-> XML whitespace handling).

(Remark: OutputKeys.INDENT is not the correct answer.)

Remark: Why this question is different from question 19102804 (Ensure Unix-style line endings):

  • This question refers explicitely to javax.xml.transform.Transformer and to the possibilities to influence its treatment of line endings. Question 19102804 asks for any solution, not only for one using javax.xml.transform.Transformer.

  • Question 19102804 is limited to the task of getting "Unix-style line endings". In my case the ideal solution would be a component that justs puts out the DOM model instance as it is, not touching any node (what everything so far does).

  • Changing the line.separator system property is not an option (see comment).

like image 421
Gustave Avatar asked Mar 03 '17 13:03

Gustave


1 Answers

If all you want to do is serialize a DOM node then in the Java world you can use LSSerializer (https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/ls/LSSerializer.html) instead of a default Transformer and then you have the method setNewLine (https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/ls/LSSerializer.html#setNewLine(java.lang.String)) to define or control your preferred line ending.

like image 107
Martin Honnen Avatar answered Oct 26 '22 23:10

Martin Honnen