Top Description Inners Fields Constructors Methods
org.jdom2.output

public final Class XMLOutputter

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Imports
java.io.BufferedOutputStream, .BufferedWriter, .IOException, .OutputStream, .OutputStreamWriter, .StringWriter, .Writer, java.util.List, org.jdom2.Attribute, .CDATA, .Comment, .Content, .DocType, .Document, .Element, .EntityRef, .ProcessingInstruction, .Text, org.jdom2.output.support.AbstractXMLOutputProcessor, .FormatStack, .XMLOutputProcessor

Outputs a JDOM document as a stream of bytes.

The XMLOutputter can manage many styles of document formatting, from untouched to 'pretty' printed. The default is to output the document content exactly as created, but this can be changed by setting a new Format object:

There are output(...) methods to print any of the standard JDOM classes to either a Writer or an OutputStream.

Warning

When outputting to a Writer, make sure the writer's encoding matches the encoding setting in the Format object. This ensures the encoding in which the content is written (controlled by the Writer configuration) matches the XML Character escaping as well as the encoding placed in the document's XML declaration (controlled by the XMLOutputter). Because a Writer cannot be queried for its encoding, the information must be passed to the Format manually in its constructor or via the Format#setEncoding method. The default encoding is UTF-8. If the default encoding of the platform is not UTF-8 and a default Format encoding is used, then there may be incorrectly formed characters in the output.

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

All of the output*(...) methods will flush the destination Writer or OutputStream before returning, and none of them will close() the destination.

XML declarations are always printed on their own line followed by a line separator (this doesn't change the semantics of the document). To omit printing of the declaration use Format#setOmitDeclaration. To omit printing of the encoding in the declaration use Format#setOmitEncoding. Unfortunately there is currently no way to know the original encoding of the document.

Empty elements are by default printed as <empty/>, but this can be configured with Format#setExpandEmptyElements to cause them to be expanded to <empty></empty>.

If changing the Format settings are insufficient for your output needs you can customise this XMLOutputter further by setting a different XMLOutputProcessor with the setXMLOutputProcessor(XMLOutputProcessor) method or an appropriate constructor. A fully-enabled Abstract class AbstractXMLOutputProcessor is available to be further extended to your needs if all you want to do is tweak some details.

Authors
Brett McLaughlin, Jason Hunter, Jason Reid, Wolfgang Werner, Elliotte Rusty Harold, David & Will (from Post Tool Design), Dan Schaffer, Alex Chaffee, Bradley S. Huffman, Rolf Lear

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
XMLOutputter.DefaultXMLProcessor

Create a final and static instance of the AbstractXMLOutputProcessor The final part is important because it improves performance.

Field Summary

Modifier and TypeField and Description
private static final XMLOutputter.DefaultXMLProcessor
DEFAULTPROCESSOR

This constant XMLOutputProcessor is used for all non-customised XMLOutputters

private Format
private XMLOutputProcessor

Constructor Summary

AccessConstructor and Description
public
XMLOutputter(Format
The Format instance to use. This instance will be cloned() and as a consequence, changes made to the specified format instance will not be reflected in this XMLOutputter. A null input format indicates that XMLOutputter should use the default Format#getRawFormat()
format
,
XMLOutputProcessor
The XMLOutputProcessor to delegate output to. If null the XMLOutputter will use the default XMLOutputProcessor.
processor
)

This will create an XMLOutputter with the specified format characteristics.

public
XMLOutputter()

This will create an XMLOutputter with a default Format and XMLOutputProcessor.

public
XMLOutputter(XMLOutputter
the XMLOutputter to clone
that
)

This will create an XMLOutputter with the same customisations set in the given XMLOutputter instance.

public
XMLOutputter(Format
The Format instance to use. This instance will be cloned() and as a consequence, changes made to the specified format instance will not be reflected in this XMLOutputter. A null input format indicates that XMLOutputter should use the default Format#getRawFormat()
format
)

This will create an XMLOutputter with the specified format characteristics.

public
XMLOutputter(XMLOutputProcessor
The XMLOutputProcessor to delegate output to. If null the XMLOutputter will use the default XMLOutputProcessor.
processor
)

This will create an XMLOutputter with the specified XMLOutputProcessor.

Method Summary

Modifier and TypeMethod and Description
public XMLOutputter
clone()

Overrides java.lang.Object.clone.

Returns a cloned copy of this XMLOutputter.
public String

Returns:

the escaped version of the input String
escapeAttributeEntities
(String
the input String to escape
str
)

Escape any characters in the input string in such a way that the returned value is valid as output in an XML Attribute value.

public String

Returns:

the escaped version of the input String
escapeElementEntities
(String
the input String to escape
str
)

Escape any characters in the input string in such a way that the returned value is valid as output in an XML Element text.

public Format

Returns:

the current Format instance used by this XMLOutputter.
getFormat
()

Returns the current format in use by the XMLOutputter.

public XMLOutputProcessor

Returns:

the current XMLOutputProcessor instance.
getXMLOutputProcessor
()

Returns the current XMLOutputProcessor instance in use by the XMLOutputter.

private static final Writer

Returns:

An Writer (Buffered) that delegates to the specified output steam
makeWriter
(final OutputStream
The OutputStream to wrap in the writer
out
,
final Format
The format is used to obtain the Character Encoding.
format
)

Get an OutputStreamWriter, use specified encoding.

public final void
output(Document
Document to format.
doc
,
OutputStream
OutputStream to use.
out
)

This will print the Document to the given OutputStream.

public final void
output(DocType
DocType to output.
doctype
,
OutputStream
OutputStream to use.
out
)

This will print the DocType to the given OutputStream.

public final void
output(Element
Element to output.
element
,
OutputStream
OutputStream to use.
out
)

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

public final void
output(List<? extends Content>
List of nodes.
list
,
OutputStream
OutputStream to use.
out
)

This will handle printing out a list of nodes.

public final void
output(CDATA
CDATA to output.
cdata
,
OutputStream
OutputStream to use.
out
)

Print out a CDATA node.

public final void
output(Text
Text to output.
text
,
OutputStream
OutputStream to use.
out
)

Print out a Text node.

public final void
output(Comment
Comment to output.
comment
,
OutputStream
OutputStream to use.
out
)

Print out a Comment.

public final void
output(ProcessingInstruction
ProcessingInstruction to output.
pi
,
OutputStream
OutputStream to use.
out
)

Print out a ProcessingInstruction.

public void
output(EntityRef
EntityRef to output.
entity
,
OutputStream
OutputStream to use.
out
)

Print out a EntityRef.

public final void
output(Document
Document to format.
doc
,
Writer
Writer to use.
out
)

This will print the Document to the given Writer.

public final void
output(DocType
DocType to output.
doctype
,
Writer
Writer to use.
out
)

Print out the DocType.

public final void
output(Element
Element to output.
element
,
Writer
Writer to use.
out
)

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

Note

ensure the character encoding of the out Writer is set the same as the Format's encoding (see 'warning' on XMLOutputter).

public final void
output(List<? extends Content>
List of nodes.
list
,
Writer
Writer to use.
out
)

This will handle printing out a list of nodes.

public final void
output(CDATA
CDATA to output.
cdata
,
Writer
Writer to use.
out
)

Print out a CDATA node.

public final void
output(Text
Text to output.
text
,
Writer
Writer to use.
out
)

Print out a Text node.

public final void
output(Comment
Comment to output.
comment
,
Writer
Writer to use.
out
)

Print out a Comment.

public final void
output(ProcessingInstruction
ProcessingInstruction to output.
pi
,
Writer
Writer to use.
out
)

Print out a ProcessingInstruction.

public final void
output(EntityRef
EntityRef to output.
entity
,
Writer
Writer to use.
out
)

Print out an EntityRef.

public final void
outputElementContent(Element
Element to output.
element
,
OutputStream
OutputStream to use.
out
)

This will handle printing out an Element's content only, not including its tag, and attributes.

public final void
outputElementContent(Element
Element to output.
element
,
Writer
Writer to use.
out
)

This will handle printing out an Element's content only, not including its tag, and attributes.

public final String

Returns:

the input content formatted as an XML String.
outputElementContentString
(Element
Element to output.
element
)

This will handle printing out an Element's content only, not including its tag, and attributes.

public final String

Returns:

the input content formatted as an XML String.
outputString
(Document
Document to format.
doc
)

Return a string representing a Document.

public final String

Returns:

the input content formatted as an XML String.
outputString
(DocType
DocType to format.
doctype
)

Return a string representing a DocType.

public final String

Returns:

the input content formatted as an XML String.
outputString
(Element
Element to format.
element
)

Return a string representing an Element.

public final String

Returns:

the input content formatted as an XML String.
outputString
(List<? extends Content>
List to format.
list
)

Return a string representing a List of Content nodes.

public final String

Returns:

the input content formatted as an XML String.
outputString
(CDATA
CDATA to format.
cdata
)

Return a string representing a CDATA node.

public final String

Returns:

the input content formatted as an XML String.
outputString
(Text
Text to format.
text
)

Return a string representing a Text node.

public final String

Returns:

the input content formatted as an XML String.
outputString
(Comment
Comment to format.
comment
)

Return a string representing a Comment.

public final String

Returns:

the input content formatted as an XML String.
outputString
(ProcessingInstruction
ProcessingInstruction to format.
pi
)

Return a string representing a ProcessingInstruction.

public final String

Returns:

the input content formatted as an XML String.
outputString
(EntityRef
EntityRef to format.
entity
)

Return a string representing an EntityRef.

public void
setFormat(Format
the format to use for subsequent output
newFormat
)

Sets the new format logic for the XMLOutputter.

public void
setXMLOutputProcessor(XMLOutputProcessor
the new XMLOutputProcesor to use for output
processor
)

Sets a new XMLOutputProcessor instance for this XMLOutputter.

public String

Returns:

a string listing the settings for this XMLOutputter instance
toString
()

Overrides java.lang.Object.toString.

Return a string listing of the settings for this XMLOutputter instance.
Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait