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:
Format#getPrettyFormat().
Format#getCompactFormat().
Format#getRawFormat().
There are methods to print any of
the standard JDOM classes to either a Writer or an OutputStream.output(.
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 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.
Format#setEncoding
The methods are for
convenience only; for top performance you should call one of the outputString(. methods and pass in your own Writer or
OutputStream if possible.
output(.
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
. To omit printing of the
encoding in the declaration use Format#setOmitDeclaration.
Unfortunately there is currently no way to know the original encoding of the
document.
Format#setOmitEncoding
Empty elements are by default printed as <empty/>, but this can be
configured with to cause
them to be expanded to <empty></empty>.
Format#setExpandEmptyElements
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.
| Modifier and Type | Class 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. |
| Modifier and Type | Field and Description |
|---|---|
| private static final XMLOutputter. | DEFAULTPROCESSOR
This constant XMLOutputProcessor is used for all non-customised XMLOutputters |
| private Format | |
| private XMLOutputProcessor |
| Access | Constructor 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, XMLOutputProcessor Format#getRawFormat()The XMLOutputProcessor to delegate output to. If null the
XMLOutputter will use the default XMLOutputProcessor. processor)This will create an |
| public | |
| public | XMLOutputter(XMLOutputter
the XMLOutputter to clone that)This will create an |
| 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)Format#getRawFormat()This will create an |
| public | XMLOutputter(XMLOutputProcessor
The XMLOutputProcessor to delegate output to. If null the
XMLOutputter will use the default XMLOutputProcessor. processor)This will create an |
| Modifier and Type | Method and Description |
|---|---|
| public XMLOutputter | |
| public String | Returns: the escaped version of the input Stringthe 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 Stringthe 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.Returns the current format in use by the XMLOutputter. |
| public XMLOutputProcessor | Returns: the current XMLOutputProcessor instance.Returns the current XMLOutputProcessor instance in use by the XMLOutputter. |
| private static final Writer | Returns: An Writer (Buffered) that delegates to the specified output steamThe 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.OutputStream to use.This will print the |
| public final void | output(DocType
DocType to output.OutputStream to use.This will print the |
| public final void | output(Element
Element to output.OutputStream to use.Print out an |
| public final void | output(List<? extends Content>
List of nodes.OutputStream to use.This will handle printing out a list of nodes. |
| public final void | |
| public final void | |
| public final void | |
| public final void | output(ProcessingInstruction
ProcessingInstruction to output.OutputStream to use.Print out a |
| public void | output(EntityRef
EntityRef to output.OutputStream to use.Print out a |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | output(ProcessingInstruction
ProcessingInstruction to output.Writer to use.Print out a |
| public final void | |
| public final void | outputElementContent(Element
Element to output.OutputStream to use.This will handle printing out an |
| public final void | outputElementContent(Element
Element to output.Writer to use.This will handle printing out an |
| public final String | Returns: the input content formatted as an XML String.Element to output.This will handle printing out an |
| public final String | Returns: the input content formatted as an XML String.Document to format.Return a string representing a |
| public final String | Returns: the input content formatted as an XML String.DocType to format.Return a string representing a |
| public final String | Returns: the input content formatted as an XML String.Element to format.Return a string representing an |
| public final String | Returns: the input content formatted as an XML String.List to format.Return a string representing a List of |
| public final String | Returns: the input content formatted as an XML String.CDATA to format.Return a string representing a |
| public final String | Returns: the input content formatted as an XML String.Text to format.Return a string representing a |
| public final String | Returns: the input content formatted as an XML String.Comment to format.Return a string representing a |
| public final String | Returns: the input content formatted as an XML String.ProcessingInstruction to format.Return a string representing a |
| public final String | Returns: the input content formatted as an XML String.EntityRef to format.Return a string representing an |
| public void | |
| 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 instanceOverrides java. |