public final class XMLOutputter
extends java.lang.Object
implements java.lang.Cloneable
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(java.lang.String)
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(boolean)
.
Unfortunately there is currently no way to know the original encoding of the
document.
Format.setOmitEncoding(boolean)
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(boolean)
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.
Constructor and Description |
---|
XMLOutputter()
|
XMLOutputter(Format format)
This will create an
XMLOutputter with the specified format
characteristics. |
XMLOutputter(Format format,
XMLOutputProcessor processor)
This will create an
XMLOutputter with the specified format
characteristics. |
XMLOutputter(XMLOutputProcessor processor)
This will create an
XMLOutputter with the specified
XMLOutputProcessor. |
XMLOutputter(XMLOutputter that)
This will create an
XMLOutputter with the same
customisations set in the given XMLOutputter instance. |
Modifier and Type | Method and Description |
---|---|
XMLOutputter |
clone()
Returns a cloned copy of this XMLOutputter.
|
java.lang.String |
escapeAttributeEntities(java.lang.String 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.
|
java.lang.String |
escapeElementEntities(java.lang.String 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.
|
Format |
getFormat()
Returns the current format in use by the XMLOutputter.
|
XMLOutputProcessor |
getXMLOutputProcessor()
Returns the current XMLOutputProcessor instance in use by the
XMLOutputter.
|
void |
output(CDATA cdata,
java.io.OutputStream out)
Print out a
node. |
void |
output(CDATA cdata,
java.io.Writer out)
Print out a
node. |
void |
output(Comment comment,
java.io.OutputStream out)
Print out a
. |
void |
output(Comment comment,
java.io.Writer out)
Print out a
. |
void |
output(DocType doctype,
java.io.OutputStream out)
This will print the
to the given
OutputStream. |
void |
output(DocType doctype,
java.io.Writer out)
Print out the
. |
void |
output(Document doc,
java.io.OutputStream out)
This will print the
to the given
OutputStream. |
void |
output(Document doc,
java.io.Writer out)
This will print the
Document to the given Writer. |
void |
output(Element element,
java.io.OutputStream out)
|
void |
output(Element element,
java.io.Writer out)
|
void |
output(EntityRef entity,
java.io.OutputStream out)
Print out a
. |
void |
output(EntityRef entity,
java.io.Writer out)
Print out an
. |
void |
output(java.util.List<? extends Content> list,
java.io.OutputStream out)
This will handle printing out a list of nodes.
|
void |
output(java.util.List<? extends Content> list,
java.io.Writer out)
This will handle printing out a list of nodes.
|
void |
output(ProcessingInstruction pi,
java.io.OutputStream out)
Print out a
. |
void |
output(ProcessingInstruction pi,
java.io.Writer out)
Print out a
. |
void |
output(Text text,
java.io.OutputStream out)
Print out a
node. |
void |
output(Text text,
java.io.Writer out)
Print out a
node. |
void |
outputElementContent(Element element,
java.io.OutputStream out)
This will handle printing out an
's content only, not including its tag, and attributes. |
void |
outputElementContent(Element element,
java.io.Writer out)
This will handle printing out an
's content only, not including its tag, and attributes. |
java.lang.String |
outputElementContentString(Element element)
This will handle printing out an
's content only, not including its tag, and attributes. |
java.lang.String |
outputString(CDATA cdata)
Return a string representing a
CDATA node. |
java.lang.String |
outputString(Comment comment)
Return a string representing a
Comment . |
java.lang.String |
outputString(DocType doctype)
Return a string representing a
DocType . |
java.lang.String |
outputString(Document doc)
Return a string representing a
Document . |
java.lang.String |
outputString(Element element)
Return a string representing an
Element . |
java.lang.String |
outputString(EntityRef entity)
Return a string representing an
EntityRef . |
java.lang.String |
outputString(java.util.List<? extends Content> list)
Return a string representing a List of
Content nodes. |
java.lang.String |
outputString(ProcessingInstruction pi)
Return a string representing a
ProcessingInstruction . |
java.lang.String |
outputString(Text text)
Return a string representing a
Text node. |
void |
setFormat(Format newFormat)
Sets the new format logic for the XMLOutputter.
|
void |
setXMLOutputProcessor(XMLOutputProcessor processor)
Sets a new XMLOutputProcessor instance for this XMLOutputter.
|
java.lang.String |
toString()
Return a string listing of the settings for this XMLOutputter instance.
|
public XMLOutputter(Format format, XMLOutputProcessor processor)
XMLOutputter
with the specified format
characteristics.
Note: the format object is cloned internally before use. If you
want to modify the Format after constructing the XMLOutputter you can
modify the Format instance getFormat()
returns.
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()
processor
- The XMLOutputProcessor to delegate output to. If null the
XMLOutputter will use the default XMLOutputProcessor.public XMLOutputter()
public XMLOutputter(XMLOutputter that)
XMLOutputter
with the same
customisations set in the given XMLOutputter
instance. Note
that XMLOutputter two = one.clone();
would work equally
well.that
- the XMLOutputter to clonepublic XMLOutputter(Format format)
XMLOutputter
with the specified format
characteristics.
Note: the format object is cloned internally before use.
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()
public XMLOutputter(XMLOutputProcessor processor)
XMLOutputter
with the specified
XMLOutputProcessor.processor
- The XMLOutputProcessor to delegate output to. If null the
XMLOutputter will use the default XMLOutputProcessor.public void setFormat(Format newFormat)
newFormat
- the format to use for subsequent outputgetFormat()
public Format getFormat()
public XMLOutputProcessor getXMLOutputProcessor()
public void setXMLOutputProcessor(XMLOutputProcessor processor)
processor
- the new XMLOutputProcesor to use for outputpublic final void output(Document doc, java.io.OutputStream out) throws java.io.IOException
Document
to the given
OutputStream. The characters are printed using the encoding specified in
the constructor, or a default of UTF-8.doc
- Document
to format.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(DocType doctype, java.io.OutputStream out) throws java.io.IOException
DocType
to the given
OutputStream.doctype
- DocType
to output.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(Element element, java.io.OutputStream out) throws java.io.IOException
element
- Element
to output.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void outputElementContent(Element element, java.io.OutputStream out) throws java.io.IOException
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>".element
- Element
to output.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(java.util.List<? extends Content> list, java.io.OutputStream out) throws java.io.IOException
The list is assumed to contain legal JDOM nodes. If other content is coerced on to the list it will cause ClassCastExceptions, and null Lists or null list members will cause NullPointerException.
list
- List
of nodes.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.ClassCastException
- if non-Content
is forced in to the listjava.lang.NullPointerException
- if the List is null or contains null members.public final void output(CDATA cdata, java.io.OutputStream out) throws java.io.IOException
CDATA
node.cdata
- CDATA
to output.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(Text text, java.io.OutputStream out) throws java.io.IOException
Text
node. Performs the necessary entity
escaping and whitespace stripping.text
- Text
to output.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(Comment comment, java.io.OutputStream out) throws java.io.IOException
Comment
.comment
- Comment
to output.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(ProcessingInstruction pi, java.io.OutputStream out) throws java.io.IOException
ProcessingInstruction
.pi
- ProcessingInstruction
to output.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public void output(EntityRef entity, java.io.OutputStream out) throws java.io.IOException
EntityRef
.entity
- EntityRef
to output.out
- OutputStream
to use.java.io.IOException
- if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final java.lang.String outputString(Document doc)
Document
. Uses an internal
StringWriter.
Warning: a String is Unicode, which may not match the outputter's specified encoding.
doc
- Document
to format.java.lang.NullPointerException
- if the specified content is null.public final java.lang.String outputString(DocType doctype)
DocType
.
Warning: a String is Unicode, which may not match the outputter's specified encoding.
doctype
- DocType
to format.java.lang.NullPointerException
- if the specified content is null.public final java.lang.String outputString(Element element)
Element
.
Warning: a String is Unicode, which may not match the outputter's specified encoding.
element
- Element
to format.java.lang.NullPointerException
- if the specified content is null.public final java.lang.String outputString(java.util.List<? extends Content> list)
Content
nodes. Warning: a String is Unicode, which may not match the outputter's specified encoding.
list
- List
to format.java.lang.ClassCastException
- if non-Content
is forced in to the listjava.lang.NullPointerException
- if the List is null or contains null members.public final java.lang.String outputString(CDATA cdata)
CDATA
node.
Warning: a String is Unicode, which may not match the outputter's specified encoding.
cdata
- CDATA
to format.java.lang.NullPointerException
- if the specified content is null.public final java.lang.String outputString(Text text)
Text
node.
Warning: a String is Unicode, which may not match the outputter's specified encoding.
text
- Text
to format.java.lang.NullPointerException
- if the specified content is null.public final java.lang.String outputString(Comment comment)
Comment
.
Warning: a String is Unicode, which may not match the outputter's specified encoding.
comment
- Comment
to format.java.lang.NullPointerException
- if the specified content is null.public final java.lang.String outputString(ProcessingInstruction pi)
ProcessingInstruction
.
Warning: a String is Unicode, which may not match the outputter's specified encoding.
pi
- ProcessingInstruction
to format.java.lang.NullPointerException
- if the specified content is null.public final java.lang.String outputString(EntityRef entity)
EntityRef
.
Warning: a String is Unicode, which may not match the outputter's specified encoding.
entity
- EntityRef
to format.java.lang.NullPointerException
- if the specified content is null.public final java.lang.String outputElementContentString(Element element)
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>".
Warning: a String is Unicode, which may not match the outputter's specified encoding.
element
- Element
to output.java.lang.NullPointerException
- if the specified content is null.public final void output(Document doc, java.io.Writer out) throws java.io.IOException
Document
to the given Writer.
Note: ensure the character encoding of the
out
Writer is set the same as the Format
's encoding
(see 'warning' on XMLOutputter
).
doc
- Document
to format.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(DocType doctype, java.io.Writer out) throws java.io.IOException
DocType
.
Note: ensure the character encoding of the
out
Writer is set the same as the Format
's encoding
(see 'warning' on XMLOutputter
).
doctype
- DocType
to output.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(Element element, java.io.Writer out) throws java.io.IOException
Element
, including its
Attribute
s, 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
).
element
- Element
to output.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void outputElementContent(Element element, java.io.Writer out) throws java.io.IOException
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>".
Note: ensure the character encoding of the
out
Writer is set the same as the Format
's encoding
(see 'warning' on XMLOutputter
).
element
- Element
to output.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(java.util.List<? extends Content> list, java.io.Writer out) throws java.io.IOException
Note: ensure the character encoding of the
out
Writer is set the same as the Format
's encoding
(see 'warning' on XMLOutputter
).
list
- List
of nodes.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(CDATA cdata, java.io.Writer out) throws java.io.IOException
CDATA
node.
Note: ensure the character encoding of the
out
Writer is set the same as the Format
's encoding
(see 'warning' on XMLOutputter
).
cdata
- CDATA
to output.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(Text text, java.io.Writer out) throws java.io.IOException
Text
node. Performs the necessary entity
escaping and whitespace stripping.
Note: ensure the character encoding of the
out
Writer is set the same as the Format
's encoding
(see 'warning' on XMLOutputter
).
text
- Text
to output.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(Comment comment, java.io.Writer out) throws java.io.IOException
Comment
.
Note: ensure the character encoding of the
out
Writer is set the same as the Format
's encoding
(see 'warning' on XMLOutputter
).
comment
- Comment
to output.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(ProcessingInstruction pi, java.io.Writer out) throws java.io.IOException
ProcessingInstruction
.
Note: ensure the character encoding of the
out
Writer is set the same as the Format
's encoding
(see 'warning' on XMLOutputter
).
pi
- ProcessingInstruction
to output.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public final void output(EntityRef entity, java.io.Writer out) throws java.io.IOException
EntityRef
.
Note: ensure the character encoding of the
out
Writer is set the same as the Format
's encoding
(see 'warning' on XMLOutputter
).
entity
- EntityRef
to output.out
- Writer
to use.java.io.IOException
- - if there's any problem writing.java.lang.NullPointerException
- if the specified content is null.public java.lang.String escapeAttributeEntities(java.lang.String str)
str
- the input String to escapepublic java.lang.String escapeElementEntities(java.lang.String str)
str
- the input String to escapepublic XMLOutputter clone()
clone
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2021 Jason Hunter, Brett McLaughlin. All Rights Reserved.