Top Description Inners Fields Constructors Methods
org.jdom.output

public Class XMLOutputter

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Imports
java.io.*, java.util.*, javax.xml.transform.Result, org.jdom.*

Outputs a JDOM document as a stream of bytes. The outputter 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. For pretty-print output, use Format#getPrettyFormat(). For whitespace-normalized output, use Format#getCompactFormat().

There are output(...) methods to print any of the standard JDOM classes, including Document and Element, 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 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.

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.

XML declarations are always printed on their own line followed by a line seperator (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. Unfortunatly 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>.

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
Version
$Revision: 1.117 $, $Date: 2009/07/23 05:54:23 $

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
XMLOutputter.NamespaceStack

Our own null subclass of NamespaceStack.

Field Summary

Modifier and TypeField and Description
protected Format
private static final String
private boolean
escapeOutput

Whether output escaping is enabled for the being processed Element - default is true

protected static final Format
private Format

Constructor Summary

AccessConstructor and Description
public
XMLOutputter()

This will create an XMLOutputter with the default Format matching Format#getRawFormat.

public
XMLOutputter(Format format)

This will create an XMLOutputter with the specified format characteristics.

public
XMLOutputter(XMLOutputter
the XMLOutputter to clone
that
)

This will create an XMLOutputter with all the options as set in the given XMLOutputter.

Method Summary

Modifier and TypeMethod and Description
public Object
clone()

Overrides java.lang.Object.clone.

Returns a copy of this XMLOutputter.
private XMLOutputter.NamespaceStack
createNamespaceStack()

Factory for making new NamespaceStack objects.

private boolean
public String

Returns:

String with escaped content.
escapeAttributeEntities
(String
String input to escape.
str
)

This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.

public String

Returns:

String with escaped content.
escapeElementEntities
(String
String input to escape.
str
)

This will take the three pre-defined entities in XML 1.0 (used specifically in XML elements) and convert their character representation to the appropriate entity reference, suitable for XML element content.

public Format
getFormat()

Returns the current format in use by the outputter.

private void
indent(Writer
Writer to use
out
,
int
current indent level
level
)

This will print indents only if indent is not null or the empty string.

private boolean
private Writer
makeWriter(OutputStream out)

Get an OutputStreamWriter, using prefered encoding (see Format#setEncoding).

private static Writer
makeWriter(OutputStream out, String enc)

Get an OutputStreamWriter, use specified encoding.

private void
newline(Writer
Writer to use
out
)

This will print a newline only if indent is not null.

private static int
nextNonText(List<E> content, int start)

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

This will print the Document to the given output stream.

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

Print out the DocType.

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

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

public void
output(List<E>
List of nodes.
list
,
OutputStream
OutputStream to use.
out
)

This will handle printing out a list of nodes.

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

Print out a CDATA node.

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

Print out a Text node.

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

Print out a Comment.

public 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 void
output(Document
Document to format.
doc
,
Writer
Writer to use.
out
)

This will print the Document to the given Writer.

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

Print out the DocType.

public 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.

public void
output(List<E>
List of nodes.
list
,
Writer
Writer to use.
out
)

This will handle printing out a list of nodes.

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

Print out a CDATA node.

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

Print out a Text node.

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

Print out a Comment.

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

Print out a ProcessingInstruction.

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

Print out a EntityRef.

public 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 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 String
outputString(Document
Document to format.
doc
)

Return a string representing a document.

public String
outputString(DocType
DocType to format.
doctype
)

Return a string representing a DocType.

public String
outputString(Element
Element to format.
element
)

Return a string representing an element.

public String
outputString(List<E>
List to format.
list
)

Return a string representing a list of nodes.

public String
outputString(CDATA
CDATA to format.
cdata
)

Return a string representing a CDATA node.

public String
outputString(Text
Text to format.
text
)

Return a string representing a Text node.

public String
outputString(Comment
Comment to format.
comment
)

Return a string representing a comment.

public String
outputString(ProcessingInstruction
ProcessingInstruction to format.
pi
)

Return a string representing a PI.

public String
outputString(EntityRef
EntityRef to format.
entity
)

Return a string representing an entity.

private void
protected void
printAttributes(Writer
Writer to use
out
,
List<E>
List of Attribute objcts
attributes
,
Element parent, XMLOutputter.NamespaceStack namespaces)

This will handle printing of a Attribute list.

protected void
printCDATA(Writer
Writer to use.
out
,
CDATA
CDATA to output.
cdata
)

This will handle printing of CDATA text.

protected void
printComment(Writer
Writer to use.
out
,
Comment
Comment to write.
comment
)

This will handle printing of comments.

private void
printContentRange(Writer
Writer to use.
out
,
List<E>
List of content to output
content
,
int
index of first content node (inclusive.
start
,
int
index of last content node (exclusive).
end
,
int
int level of indentation.
level
,
XMLOutputter.NamespaceStack
List stack of Namespaces in scope.
namespaces
)

This will handle printing of content within a given range.

protected void
printDeclaration(Writer
Writer to use.
out
,
Document
Document whose declaration to write.
doc
,
String
The encoding to add to the declaration
encoding
)

This will handle printing of the declaration.

protected void
printDocType(Writer
Writer to use.
out
,
DocType
Document whose declaration to write.
docType
)

This handle printing the DOCTYPE declaration if one exists.

protected void
printElement(Writer
Writer to use.
out
,
Element
Element to output.
element
,
int
int level of indention.
level
,
XMLOutputter.NamespaceStack
List stack of Namespaces in scope.
namespaces
)

This will handle printing of a Element, its Attributes, and all contained (child) elements, etc.

private void
protected void
printEntityRef(Writer
Writer to use.
out
,
EntityRef
EntityRef to output.
entity
)

This will handle printing a EntityRef.

private void
printNamespace(Writer
Writer to use.
out
,
Namespace
Namespace to print definition of
ns
,
XMLOutputter.NamespaceStack namespaces)

This will handle printing of any needed Namespace declarations.

protected void
printProcessingInstruction(Writer
Writer to use.
out
,
ProcessingInstruction
ProcessingInstruction to write.
pi
)

This will handle printing of processing instructions.

private void
private void
private void
printString(Writer out, String str)

This will handle printing a string.

protected void
printText(Writer
Writer to use.
out
,
Text
Text to write.
text
)

This will handle printing of Text strings.

private void
printTextRange(Writer
Writer to use.
out
,
List<E>
List of content to output
content
,
int
index of first content node (inclusive).
start
,
int
index of last content node (exclusive).
end
)

This will handle printing of a sequence of CDATA or Text nodes.

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

Sets the new format logic for the outputter.

private int
skipLeadingWhite(List<E> content, int start)

private int
skipTrailingWhite(List<E> content, int start)

private boolean
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