org.jdom.output
Class XMLOutputter


public class XMLOutputter
implements Cloneable

XMLOutputter takes a JDOM tree and formats it to a stream as XML. This formatter performs typical document formatting. The XML declaration and processing instructions are always on their own lines. Empty elements are printed as <empty/> and text-only contents are printed as <tag>content</tag> on a single line. Constructor parameters control the indent amount and whether new lines are printed between elements. The other parameters are configurable through the set* methods.

For compact machine-readable output create a default XMLOutputter and call setTrimText(true) to strip any whitespace that was preserved from the source.

There are output(...) methods to print any of the standard JDOM classes, including Document and Element, to either a Writer or an OutputStream. Warning: using your own Writer may cause the outputter's preferred character encoding to be ignored. If you use encodings other than UTF8, we recommend using the method that takes an OutputStream instead.

The methods outputString(...) are for convenience only; for top performance you should call output(...) and pass in your own Writer or OutputStream to if possible.

Author:
Brett McLaughlin, Jason Hunter, Jason Reid, Wolfgang Werner, Elliotte Rusty Harold, David & Will (from Post Tool Design), Dan Schaffer, Alex Chaffee (alex@jguru.com)
Version: 1.0

Constructor Summary
XMLOutputter()
           This will create an XMLOutputter with no additional whitespace (indent or new lines) added; the whitespace from the element text content is fully preserved.
XMLOutputter(String indent)
           This will create an XMLOutputter with the given indent added but no new lines added; all whitespace from the element text content is included as well.
XMLOutputter(String indent, boolean newlines)
           This will create an XMLOutputter with the given indent that prints newlines only if newlines is true; all whitespace from the element text content is included as well.
XMLOutputter(String indent, boolean newlines, String encoding)
           This will create an XMLOutputter with the given indent and new lines printing only if newlines is true, and encoding format encoding.
XMLOutputter(XMLOutputter that)
           This will create an XMLOutputter with all the options as set in the given XMLOutputter.

Method Summary
 voidoutput(Document doc, OutputStream out)
           This will print the Document to the given output stream.
 voidoutput(Document doc, Writer writer)
           This will print the Document to the given Writer.
 voidoutput(Element element, Writer out)
           Print out an Element, including its Attributes, and its value, and all contained (child) elements etc.
 voidoutput(Element element, OutputStream out)
           Print out an Element, including its Attributes, and its value, and all contained (child) elements etc.
 voidoutput(CDATA cdata, Writer out)
           Print out a CDATA
 voidoutput(CDATA cdata, OutputStream out)
           Print out a CDATA
 voidoutput(Comment comment, Writer out)
           Print out a Comment
 voidoutput(Comment comment, OutputStream out)
           Print out a Comment
 voidoutput(String string, Writer out)
           Print out a String.
 voidoutput(String string, OutputStream out)
           Print out a String.
 voidoutput(Entity entity, Writer out)
           Print out an Entity.
 voidoutput(Entity entity, OutputStream out)
           Print out an Entity.
 voidoutput(ProcessingInstruction pi, Writer out)
           Print out a ProcessingInstruction
 voidoutput(ProcessingInstruction pi, OutputStream out)
           Print out a ProcessingInstruction
 voidoutputElementContent(Element element, Writer out)
           This will handle printing out an Element's content only, not including its tag, and attributes.
 StringoutputString(Document doc)
          Return a string representing a document.
 StringoutputString(Element element)
          Return a string representing an element.
 intparseArgs(String[] args, int i)
          parse command-line arguments of the form -omitEncoding -indentSize 3 ...
 voidsetEncoding(String encoding)
           
 voidsetExpandEmptyElements(boolean expandEmptyElements)
           This will set whether empty elements are expanded from <tagName> to <tagName></tagName>.
 voidsetIndent(String indent)
           This will set the indent String to use; this is usually a String of empty spaces.
 voidsetIndent(boolean doIndent)
          Set the indent on or off.
 voidsetIndentLevel(int indentLevel)
          Set the initial indentation level.
 voidsetIndentSize(int indentSize)
           This will set the indent String's size; an indentSize of 4 would result in the indention being equivalent to the String "    " (four space chars).
 voidsetLineSeparator(String separator)
          This will set the new-line separator.
 voidsetNewlines(boolean newlines)
           
 voidsetOmitEncoding(boolean omitEncoding)
           This will set whether the XML declaration (<?xml version="1.0" encoding="UTF-8"?>) includes the encoding of the document.
 voidsetPadText(boolean padText)
           Ensure that text immediately preceded by or followed by an element will be "padded" with a single space.
 voidsetSuppressDeclaration(boolean suppressDeclaration)
           This will set whether the XML declaration (<?xml version="1.0"?>) will be suppressed or not.
 voidsetTrimText(boolean trimText)
           This will set whether the text is output verbatim (false) or with whitespace stripped as per org.jdom.Element.getTextTrim().

Constructor Detail

XMLOutputter

public XMLOutputter()

This will create an XMLOutputter with no additional whitespace (indent or new lines) added; the whitespace from the element text content is fully preserved.


XMLOutputter

public XMLOutputter(String indent)

This will create an XMLOutputter with the given indent added but no new lines added; all whitespace from the element text content is included as well.

Parameters:
indent - the indent string, usually some number of spaces

XMLOutputter

public XMLOutputter(String indent, boolean newlines)

This will create an XMLOutputter with the given indent that prints newlines only if newlines is true; all whitespace from the element text content is included as well.

Parameters:
indent - the indent String, usually some number of spaces
newlines - true indicates new lines should be printed, else new lines are ignored (compacted).

XMLOutputter

public XMLOutputter(String indent, boolean newlines, String encoding)

This will create an XMLOutputter with the given indent and new lines printing only if newlines is true, and encoding format encoding.

Parameters:
indent - the indent String, usually some number of spaces
newlines - true indicates new lines should be printed, else new lines are ignored (compacted).
encoding - set encoding format.

XMLOutputter

public XMLOutputter(XMLOutputter that)

This will create an XMLOutputter with all the options as set in the given XMLOutputter. Note that XMLOutputter two = (XMLOutputter)one.clone(); would work equally well.

Parameters:
that - the XMLOutputter to clone
Method Detail

output

public void output(Document doc, OutputStream out)
throws java.io.IOException

This will print the Document to the given output stream. The characters are printed using the encoding specified in the constructor, or a default of UTF-8.

Parameters:
doc - Document to format.
out - OutputStream to write to.
Throws:
IOException - - if there's any problem writing.

output

public void output(Document doc, Writer writer)
throws java.io.IOException

This will print the Document to the given Writer.

Warning: using your own Writer may cause the outputter's preferred character encoding to be ignored. If you use encodings other than UTF8, we recommend using the method that takes an OutputStream instead.

Note: as with all Writers, you may need to flush() yours after this method returns.

Parameters:
doc - Document to format.
out - Writer to write to.
Throws:
IOException - - if there's any problem writing.

output

public void output(Element element, Writer out)
throws java.io.IOException

Print out an Element, including its Attributes, and its value, and all contained (child) elements etc.

Parameters:
element - Element to output.
out - Writer to write to.

output

public void output(Element element, OutputStream out)
throws java.io.IOException

Print out an Element, including its Attributes, and its value, and all contained (child) elements etc.

Parameters:
element - Element to output.
out - Writer to write to.

output

public void output(CDATA cdata, Writer out)
throws java.io.IOException

Print out a CDATA

Parameters:
cdata - CDATA to output.
out - Writer to write to.

output

public void output(CDATA cdata, OutputStream out)
throws java.io.IOException

Print out a CDATA

Parameters:
cdata - CDATA to output.
out - OutputStream to write to.

output

public void output(Comment comment, Writer out)
throws java.io.IOException

Print out a Comment

Parameters:
comment - Comment to output.
out - Writer to write to.

output

public void output(Comment comment, OutputStream out)
throws java.io.IOException

Print out a Comment

Parameters:
comment - Comment to output.
out - OutputStream to write to.

output

public void output(String string, Writer out)
throws java.io.IOException

Print out a String. Perfoms the necessary entity escaping and whitespace stripping.

Parameters:
string - String to output.
out - Writer to write to.

output

public void output(String string, OutputStream out)
throws java.io.IOException

Print out a String. Perfoms the necessary entity escaping and whitespace stripping.

Parameters:
cdata - CDATA to output.
out - OutputStream to write to.

output

public void output(Entity entity, Writer out)
throws java.io.IOException

Print out an Entity.

Parameters:
entity - Entity to output.
out - Writer to write to.

output

public void output(Entity entity, OutputStream out)
throws java.io.IOException

Print out an Entity.

Parameters:
cdata - CDATA to output.
out - OutputStream to write to.

output

public void output(ProcessingInstruction pi, Writer out)
throws java.io.IOException

Print out a ProcessingInstruction

Parameters:
element - ProcessingInstruction to output.
out - Writer to write to.

output

public void output(ProcessingInstruction pi, OutputStream out)
throws java.io.IOException

Print out a ProcessingInstruction

Parameters:
processingInstruction - ProcessingInstruction to output.
out - OutputStream to write to.

outputElementContent

public void outputElementContent(Element element, Writer out)
throws java.io.IOException

This will handle printing out an Element's content only, not including its tag, and attributes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".

Parameters:
element - Element to output.
out - Writer to write to.
indent - int level of indention.

outputString

public String outputString(Document doc)
throws java.io.IOException
Return a string representing a document. Uses an internal StringWriter. Warning: a String is Unicode, which may not match the outputter's specified encoding.
Parameters:
doc - Document to format.

outputString

public String outputString(Element element)
throws java.io.IOException
Return a string representing an element. Warning: a String is Unicode, which may not match the outputter's specified encoding.
Parameters:
doc - Element to format.

parseArgs

public int parseArgs(String[] args, int i)
parse command-line arguments of the form -omitEncoding -indentSize 3 ...
Returns: int index of first parameter that we didn't understand

setEncoding

public void setEncoding(String encoding)
Parameters:
encoding - encoding format

setExpandEmptyElements

public void setExpandEmptyElements(boolean expandEmptyElements)

This will set whether empty elements are expanded from <tagName> to <tagName></tagName>.

Parameters:
expandEmptyElements - boolean indicating whether or not empty elements should be expanded.

setIndent

public void setIndent(String indent)

This will set the indent String to use; this is usually a String of empty spaces. If you pass null, or the empty string (""), then no indentation will happen.

Default: none (null)
Parameters:
indent - String to use for indentation.

setIndent

public void setIndent(boolean doIndent)
Set the indent on or off. If setting on, will use the value of STANDARD_INDENT, which is usually two spaces.
Parameters:
doIndent - if true, set indenting on; if false, set indenting off

setIndentLevel

public void setIndentLevel(int indentLevel)
Set the initial indentation level. This can be used to output a document (or, more likely, an element) starting at a given indent level, so it's not always flush against the left margin. Default: 0
Parameters:
indentLevel - the number of indents to start with

setIndentSize

public void setIndentSize(int indentSize)

This will set the indent String's size; an indentSize of 4 would result in the indention being equivalent to the String "    " (four space chars).

Parameters:
indentSize - int number of spaces in indentation.

setLineSeparator

public void setLineSeparator(String separator)

This will set the new-line separator. The default is \r\n. Note that if the "newlines" property is false, this value is irrelevant. To make it output the system default line ending string, call setLineSeparator(System.getProperty("line.separator"))

We could change this to the System default, but I prefer not to make output platform dependent. A carriage return, linefeed pair is the most generally acceptable linebreak. Another possibility is to use only a line feed, which is XML's preferred (but not required) solution. However, both carriage return and linefeed are required for many network protocols, and the parser on the other end should normalize this. --Rusty
Parameters:
separator - String line separator to use.
See Also:
setNewlines(boolean)

setNewlines

public void setNewlines(boolean newlines)
Parameters:
newlines - true indicates new lines should be printed, else new lines are ignored (compacted).
See Also:
setLineSeparator(java.lang.String)

setOmitEncoding

public void setOmitEncoding(boolean omitEncoding)

This will set whether the XML declaration (<?xml version="1.0" encoding="UTF-8"?>) includes the encoding of the document. It is common to suppress this in uses such as WML and other wireless device protocols.

Parameters:
omitEncoding - boolean indicating whether or not the XML declaration should indicate the document encoding.

setPadText

public void setPadText(boolean padText)

Ensure that text immediately preceded by or followed by an element will be "padded" with a single space. This is used to allow make browser-friendly HTML, avoiding trimText's transformation of, e.g., The quick <b>brown</b> fox into The quick<b>brown</b>fox (the latter will run the three separate words together into a single word). This setting is not too useful if you haven't also called setTrimText(boolean).

Default: false

Parameters:
padText - boolean if true, pad string-element boundaries

setSuppressDeclaration

public void setSuppressDeclaration(boolean suppressDeclaration)

This will set whether the XML declaration (<?xml version="1.0"?>) will be suppressed or not. It is common to suppress this in uses such as SOAP and XML-RPC calls.

Parameters:
suppressDeclaration - boolean indicating whether or not the XML declaration should be suppressed.

setTrimText

public void setTrimText(boolean trimText)

This will set whether the text is output verbatim (false) or with whitespace stripped as per org.jdom.Element.getTextTrim().

Default: false

Parameters:
trimText - boolean true=>trim the whitespace, false=>use text verbatim

Association Links

to Class java.lang.String

standard value to indent by, if we are indenting *

to Class java.lang.String

The encoding format

to Class java.lang.String

The default indent is no spaces (as original document)

to Class java.lang.String

New line separator

to Class java.lang.String