I'm using Apache Xerces 2.11.0 and Apache Xalan 2.7.1 and I'm having problems with additional carriage return characters in the serialized XML.
I have this (pseudo) code:
String myString = ...;
Document doc = ...;
Element item = doc.createElement("item");
item.appendChild(doc.createCDATASection(myString));
Transformer transformer = ...;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Result result = new StreamResult(stream);
transformer.transform(new DOMSource(document), result);
Now myString
contains line breaks (\r\n
), (actually it's base64 encoded data) but when I look at the serialized output, there are additional \r
characters.
Input:
Line 1 \r\n
Line 2 \r\n
Line 3 \r\n
Output:
Line 1 \r\r\n
Line 2 \r\r\n
Line 3 \r\r\n
If I use createTextNode
instead of createCDATASection
the output becomes even more interesting:
Line 1 \r\n
Line 2 \r\n
Line 3 \r\n
The additional character seems to be introduced during serialization, the DOM tree seems to be correct. (According to getTextContent()
)
Why is this happening? What can I do to fix this?
Result
is just an output tree. How are you serializing Result to a String or output stream? – Utile