Writing XML Documents with JDOM

Once you've created a document, you're likely to want to serialize it to a network socket, a file, a string, or some other stream. JDOM's org.jdom.output.XMLOutputter class does this in a standard way. You can create an XMLOutputter object with a no-args constructor and then write a document onto an OutputStream with its output() method. For example, the following code fragment writes the Document object named doc onto System.out:

XMLOutputter outputter = new XMLOutputter(); 
try {
  outputter.output(doc, System.out);
}
catch (IOException e) {
  System.err.println(e);
}

Although you also can output a document onto a java.io.Writer, it's recommended that you use an OutputStream because it's generally not possible to ...

Get Processing XML with Java™: A Guide to SAX, DOM, JDOM, JAXP, and TrAX now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.