Outputting a Document

The process of outputting a JDOM Document object is even simpler than the process of creating one. The org.jdom.output package provides helper and utility classes for outputting a Document to various sources. No interface is provided to define required behavior, as output of a Document can be used in a variety of ways, from something as simple as writing to a file to something as complex as triggering events for another application component to use.

Standard XML Output

The most common use for XML data within a JDOM Document is to output that data as XML to a file or another application component, using an OutputStream . Of course, this stream may wrap a console’s output, a file, a URL, or any other construct that can receive data. This task is handled in JDOM by the org.jdom.output.XMLOutputter class. This class provides the following constructors and output method:

public class XMLOutputter {

    // Accept defaults: 2 space indent and new line feeds on
    public XMLOutputter(  );

    // Specify indent, accept default for new line feeds (on)
    public XMLOutputter(String indent);

    // Specify the indention to use and if new line feeds should be used
    public XMLOutputter(String indent, boolean newlines);

    // Output a JDOM Document to a stream
    public void output(Document doc, OutputStream out)throws IOException;

}

When instantiated with the default constructor, this results in a “pretty printing” of the JDOM Document; other options can be supplied for more compact output (such ...

Get Java and XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.