White space is significant in XML and therefore in JDOM.
import org.jdom.*;
import org.jdom.output.XMLOutputter;
import java.io.IOException;
public class PrettyHelloJDOM {
public static void main(String[] args) {
Element root = new Element("GREETING");
root.setText("\n Hello JDOM!\n");
Document doc = new Document(root);
// At this point the document only exists in memory.
// We still need to serialize it
XMLOutputter outputter = new XMLOutputter();
try {
outputter.output(doc, System.out);
}
catch (IOException e) {
System.err.println(e);
}
}
}
If white space is not significant in your application, you can instruct the outputter to clean it up for you.