I'm building an XML document and printing out into an indented format using the JVM build-in libraries. When there is a text node in the document that contains a line break, it wraps the line to the start of the line instead of it's proper indented position
sample code
ByteArrayOutputStream s;
s = new ByteArrayOutputStream();
Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Transformer t = TransformerFactory.newInstance().newTransformer();
Element a;
Text b;
a = d.createElement("a");
String text = "multi\nline\ntext";
b = d.createTextNode(text);
a.appendChild(b);
d.appendChild(a);
t.setOutputProperty(OutputKeys.INDENT,"yes");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
t.transform(new DOMSource(d), new StreamResult(s));
System.out.println(new String(s.toByteArray()));
output
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>multi
line
text</a>
desired output
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>
multi
line
text
</a>
Is there a smart way to make each new line begin at where the indented xml tag would begin? Something tells me textnode isn't the right thing to use? Is there something better?