All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----org.jdom.output.XMLOutputter
XMLOutputter
takes a JDOM tree and formats it to a
stream as XML. 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 with the various set*()
methods.
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 XMLOutputter. 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 XMLOutputter manually in its
constructor or via the setEncoding() method. The default XMLOutputter
encoding is UTF-8.
The methods outputString(...)
are for convenience
only; for top performance you should call one of the
output(...)
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 setOmitDeclaration
.
To omit printing of the encoding in the declaration use
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 setExpandEmptyElements
to cause them to
be expanded to <empty></empty>.
Several modes are available to effect the way textual content is printed. All modes are configurable through corresponding set*() methods. Below is a table which explains the modes and the effect on the resulting output.
Text Mode | Resulting behavior. |
---|---|
Default | All content is printed in the format it was created, no whitespace or line separators are are added or removed. |
TrimAllWhite | Content between tags consisting of all whitespace is not printed. If the content contains even one non-whitespace character, it is printed verbatim, whitespace and all. |
TextTrim | Same as TrimAllWhite, plus leading/trailing whitespace are trimmed. |
TextNormalize | Same as TextTrim, in addition interior whitespace is compressed to a single space. |
For pretty-print output, use setNewlines
in conjunction
with setIndent
. Setting newlines to true
causes
tags to be aligned and possibly indented. With newlines true
,
whitespace might be added back to fit alignment needs. In most cases
texual content is aligned with the surrounding tags (after the
appropriate text mode is applied). In the case where the only content
between the start and end tags is textual, the start tag, text, and end
tag are all printed on the same line.
When a element has a xml:space attribute with the value of "preserve", all formating is turned off and reverts back to the default until the element and it's contents have been printed. If a nested element contains another xml:space with the value "default" formatting is turned back on for the child element and then off for the remainder of the parent element.
This will create an XMLOutputter
with no additional
whitespace (indent or newlines) added; the whitespace from the
element text content is fully preserved.
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.
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.
This will create an XMLOutputter
with
the given indent and new lines printing only if
newlines
is true
, and encoding format
encoding
.
This will create an XMLOutputter
with all the
options as set in the given XMLOutputter
.
Factory for making new NamespaceStack objects.
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.
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.
This will print indents (only if the newlines flag was
set to true
, and indent is non-null).
Deprecated.
This will print indents (only if the newlines flag was
set to true
, and indent is non-null).
Get an OutputStreamWriter, using prefered encoding (see {
Get an OutputStreamWriter, use specified encoding.
This will print a new line only if the newlines flag was set to true.
Print out a
Print out a
Print out a
Print out a
Print out the
Print out the
This will print the
This will print the
Print out an
Print out an
Print out a
Print out a
This will handle printing out a list of nodes.
This will handle printing out a list of nodes.
Print out a
Print out a
Print out a
Print out a
Print out a
Print out a
This will handle printing out an
Return a string representing a CDATA node.
Return a string representing a comment.
Return a string representing a DocType.
Return a string representing a document.
Return a string representing an element.
Return a string representing an entity.
Return a string representing a list of nodes.
Return a string representing a PI.
Return a string representing (with trimming, normalizatio, and
escaping possibly applied) a
Return a string representing a Text node.
Parse command-line arguments of the form
This will handle printing of a
This will handle printing of
This will handle printing of comments.
This will handle printing of a
This will handle printing of content within a given range.
This will handle printing of the declaration.
This handle printing the DOCTYPE declaration if one exists.
This will handle printing of a
This will handle printing of a
This will handle printing a
This will handle printing of processing instructions.
This will handle printing of
This will handle printing of a sequence of
Sets the output encoding.
This will set whether empty elements are expanded from
Set the indent on or off, newlines must be set to
This will set the indent
This will set the indent
Set the initial indentation level.
Deprecated.
This will set the indent
This will set the newline separator (
Sets whether newlines (
This will set whether the XML declaration
(
This will set whether the XML declaration
(
Ensure that text immediately preceded by or followed by an
element will be "padded" with a single space.
Deprecated.
This will set whether the XML declaration
(
This will set whether the text is output verbatim (false)
or with whitespace normalized as per
This will set whether the text has leading/trailing whitespace
trimmed.
This will set whether content between tags consisting of all
whitespace is printed or trimmed.
Return a string listing of the settings for this
XMLOutputter instance.
{
output(CDATA, Writer)
{
output(Comment, OutputStream)
{
output(Comment, Writer)
{
output(DocType, OutputStream)
{
output(DocType, Writer)
{
output(Document, OutputStream)
Document
to the given output stream.
output(Document, Writer)
Document
to the given Writer.
output(Element, OutputStream)
{
output(Element, Writer)
{
output(EntityRef, OutputStream)
{
output(EntityRef, Writer)
{
output(List, OutputStream)
output(List, Writer)
output(ProcessingInstruction, OutputStream)
{
output(ProcessingInstruction, Writer)
{
output(String, OutputStream)
{
Deprecated.
output(String, Writer)
{
Deprecated.
output(Text, OutputStream)
{
output(Text, Writer)
{
outputElementContent(Element, OutputStream)
{
outputElementContent(Element, Writer)
{
outputString(CDATA)
outputString(Comment)
outputString(DocType)
outputString(Document)
outputString(Element)
outputString(EntityRef)
outputString(List)
outputString(ProcessingInstruction)
outputString(String)
String
.
outputString(Text)
parseArgs(String[], int)
-omitEncoding
-indentSize 3 ...
printAttributes(List, Element, Writer, XMLOutputter. NamespaceStack)
{
printCDATA(CDATA, Writer)
{
printComment(Comment, Writer)
printContent(List, Writer, int, XMLOutputter. NamespaceStack)
{
printContentRange(List, int, int, Writer, int, XMLOutputter. NamespaceStack)
printDeclaration(Document, Writer, String)
printDocType(DocType, Writer)
printElement(Element, Writer, int, XMLOutputter. NamespaceStack)
{
printElementContent(Element, Writer, int, XMLOutputter. NamespaceStack)
{
printEntityRef(EntityRef, Writer)
{
printProcessingInstruction(ProcessingInstruction, Writer)
printString(String, Writer)
printText(Text, Writer)
{
printTextRange(List, int, int, Writer)
{
setEncoding(String)
setExpandEmptyElements(boolean)
<tagName/>
to
<tagName></tagName>
.
setIndent(boolean)
true
for indentation to actually occur.
setIndent(int)
String
's size; a size
of 4 would result in the indentation being equivalent to the
String
" " (four spaces).
setIndent(String)
String
to use; this
is usually a String
of empty spaces.
setIndentLevel(int)
setIndentSize(int)
String
's size; an indentSize
of 4 would result in the indentation being equivalent to the
String
" " (four spaces).
setLineSeparator(String)
lineSeparator
).
setNewlines(boolean)
lineSeparator
) should
be printed.
setOmitDeclaration(boolean)
<?xml version="1.0"?>
)
will be omitted or not.
setOmitEncoding(boolean)
<?xml version="1.0" encoding="UTF-8"?>
)
includes the encoding of the document.
setPadText(boolean)
setSuppressDeclaration(boolean)
<?xml version="1.0"?>
)
will be suppressed or not.
Deprecated.
setTextNormalize(boolean)
{
setTextTrim(boolean)
setTrimAllWhite(boolean)
toString()
public XMLOutputter()
This will create an XMLOutputter
with no additional
whitespace (indent or newlines) added; the whitespace from the
element text content is fully preserved.
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.
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.
String
, usually some number
of spaces
true
indicates new lines should be
printed, else new lines are ignored (compacted).
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
.
String
, usually some number
of spaces
true
indicates new lines should be
printed, else new lines are ignored (compacted).
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.
public void setLineSeparator(String separator)
This will set the newline separator (lineSeparator
).
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"))
To output "UNIX-style" documents, call
setLineSeparator("\n")
. To output "Mac-style"
documents, call setLineSeparator("\r")
. DOS-style
documents use CR-LF ("\r\n"), which is the default.
Note that this only applies to newlines generated by the
outputter. If you parse an XML document that contains newlines
embedded inside a text node, and you do not call
setTextNormalize
, then the newlines will be output
verbatim, as "\n" which is how parsers normalize them.
String
line separator to use.
public void setNewlines(boolean newlines)
Sets whether newlines (lineSeparator
) should
be printed.
true
indicates new lines should be
printed, else new lines are ignored (compacted).
public void setEncoding(String encoding)
Sets the output encoding. The name should be an accepted XML encoding.
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 omit
this in uses such as WML and other wireless device protocols.
boolean
indicating whether or not
the XML declaration should indicate the document encoding.
public void setOmitDeclaration(boolean omitDeclaration)
This will set whether the XML declaration
(<?xml version="1.0"?>
)
will be omitted or not. It is common to omit this in uses such
as SOAP and XML-RPC calls.
boolean
indicating whether or not
the XML declaration should be omitted.
public void setExpandEmptyElements(boolean expandEmptyElements)
This will set whether empty elements are expanded from
<tagName/>
to
<tagName></tagName>
.
boolean
indicating whether or not
empty elements should be expanded.
public void setTrimAllWhite(boolean trimAllWhite)
This will set whether content between tags consisting of all whitespace is printed or trimmed.
Default: false
boolean
true=>content consisting of
only whitespace is not print, false=>use text verbatim
public void setTextTrim(boolean textTrim)
This will set whether the text has leading/trailing whitespace trimmed.
Default: false
boolean
true=>trim the leading/trailing
whitespace, false=>use text verbatim
public void setTextNormalize(boolean textNormalize)
This will set whether the text is output verbatim (false)
or with whitespace normalized as per {@link
org.jdom.Element#getTextNormalize()}
.
Default: false
boolean
true=>normalize the
whitespace, false=>use text verbatim
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)
String
to use for indentation.
public void setIndent(boolean doIndent)
Set the indent on or off, newlines must be set to true
for indentation to actually occur. If setting on, will use the
value of STANDARD_INDENT, which is usually two spaces.
public void setIndent(int size)
This will set the indent String
's size; a size
of 4 would result in the indentation being equivalent to the
String
" " (four spaces).
int
number of spaces in indentation.
public void setIndentSize(int indentSize)
This will set the indent String
's size; an indentSize
of 4 would result in the indentation being equivalent to the
String
" " (four spaces).
int
number of spaces in indentation.
public void output(Document doc, OutputStream out) throws 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.
Document
to format.
OutputStream
to use.
public void output(DocType doctype, OutputStream out) throws IOException
Print out the {@link DocType}
.
DocType
to output.
OutputStream
to use.
public void output(Element element, OutputStream out) throws IOException
Print out an {@link Element}
, including
it's {@link Attribute}
s, and all
contained (child) elements, etc.
Element
to output.
Writer
to use.
public void outputElementContent(Element element, OutputStream out) throws IOException
{@link
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
to output.
OutputStream
to use.
public void output(List list, OutputStream out) throws IOException
This will handle printing out a list of nodes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".
List
of nodes.
OutputStream
to use.
public void output(CDATA cdata, OutputStream out) throws IOException
Print out a {@link CDATA}
node.
CDATA
to output.
OutputStream
to use.
public void output(Text text, OutputStream out) throws IOException
Print out a {@link Text}
node. Perfoms
the necessary entity escaping and whitespace stripping.
Text
to output.
OutputStream
to use.
public void output(String string, OutputStream out) throws IOException
Text
,OutputStream
)
Print out a {@link java.lang.String}
. Perfoms
the necessary entity escaping and whitespace stripping.
String
to output.
OutputStream
to use.
public void output(Comment comment, OutputStream out) throws IOException
Print out a {@link Comment}
.
Comment
to output.
OutputStream
to use.
public void output(ProcessingInstruction pi, OutputStream out) throws IOException
Print out a {@link ProcessingInstruction}
.
ProcessingInstruction
to output.
OutputStream
to use.
public void output(EntityRef entity, OutputStream out) throws IOException
Print out a {@link EntityRef}
.
EntityRef
to output.
OutputStream
to use.
protected Writer makeWriter(OutputStream out) throws UnsupportedEncodingException
Get an OutputStreamWriter, using prefered encoding (see {@link #setEncoding}).
protected Writer makeWriter(OutputStream out, String enc) throws UnsupportedEncodingException
Get an OutputStreamWriter, use specified encoding.
public void output(Document doc, Writer out) throws 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 UTF-8, we recommend using the method that takes an OutputStream instead.
Document
to format.
Writer
to use.
public void output(DocType doctype, Writer out) throws IOException
Print out the {@link DocType}
.
DocType
to output.
Writer
to use.
public void output(Element element, Writer out) throws IOException
Print out an {@link Element}
, including
it's {@link Attribute}
s, and all
contained (child) elements, etc.
Element
to output.
Writer
to use.
public void outputElementContent(Element element, Writer out) throws IOException
This will handle printing out an {@link
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
to output.
Writer
to use.
public void output(List list, Writer out) throws IOException
This will handle printing out a list of nodes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".
List
of nodes.
Writer
to use.
public void output(CDATA cdata, Writer out) throws IOException
Print out a {@link CDATA}
node.
CDATA
to output.
Writer
to use.
public void output(Text text, Writer out) throws IOException
Print out a {@link Text}
node. Perfoms
the necessary entity escaping and whitespace stripping.
Text
to output.
Writer
to use.
public void output(String string, Writer out) throws IOException
Text
,Writer
)
Print out a {@link java.lang.String}
. Perfoms
the necessary entity escaping and whitespace stripping.
String
to output.
Writer
to use.
public void output(Comment comment, Writer out) throws IOException
Print out a {@link Comment}
.
Comment
to output.
Writer
to use.
public void output(ProcessingInstruction pi, Writer out) throws IOException
Print out a {@link ProcessingInstruction}
.
ProcessingInstruction
to output.
Writer
to use.
public void output(EntityRef entity, Writer out) throws IOException
Print out a {@link EntityRef}
.
EntityRef
to output.
Writer
to use.
public String outputString(Document doc)
Return a string representing a document. Uses an internal StringWriter. Warning: a String is Unicode, which may not match the outputter's specified encoding.
Document
to format.
public String outputString(DocType doctype)
Return a string representing a DocType. Warning: a String is Unicode, which may not match the outputter's specified encoding.
DocType
to format.
public String outputString(Element element)
Return a string representing an element. Warning: a String is Unicode, which may not match the outputter's specified encoding.
Element
to format.
public String outputString(List list)
Return a string representing a list of nodes. The list is assumed to contain legal JDOM nodes.
List
to format.
public String outputString(CDATA cdata)
Return a string representing a CDATA node. Warning: a String is Unicode, which may not match the outputter's specified encoding.
CDATA
to format.
public String outputString(Text text)
Return a string representing a Text node. Warning: a String is Unicode, which may not match the outputter's specified encoding.
Text
to format.
public String outputString(String str)
Return a string representing (with trimming, normalizatio, and
escaping possibly applied) a String
. Warning: a
String is Unicode, which may not match the outputter's specified
encoding.
String
to format.
public String outputString(Comment comment)
Return a string representing a comment. Warning: a String is Unicode, which may not match the outputter's specified encoding.
Comment
to format.
public String outputString(ProcessingInstruction pi)
Return a string representing a PI. Warning: a String is Unicode, which may not match the outputter's specified encoding.
ProcessingInstruction
to format.
public String outputString(EntityRef entity)
Return a string representing an entity. Warning: a String is Unicode, which may not match the outputter's specified encoding.
EntityRef
to format.
protected void printDeclaration(Document doc, Writer out, String encoding) throws IOException
This will handle printing of the declaration. Assumes XML version 1.0 since we don't directly know.
Document
whose declaration to write.
Writer
to use.
protected void printDocType(DocType docType, Writer out) throws IOException
This handle printing the DOCTYPE declaration if one exists.
Document
whose declaration to write.
Writer
to use.
protected void printComment(Comment comment, Writer out) throws IOException
This will handle printing of comments.
Comment
to write.
Writer
to use.
protected void printProcessingInstruction(ProcessingInstruction pi, Writer out) throws IOException
This will handle printing of processing instructions.
ProcessingInstruction
to write.
Writer
to use.
protected void printEntityRef(EntityRef entity, Writer out) throws IOException
This will handle printing a {@link EntityRef}
.
Only the entity reference such as &entity;
will be printed. However, subclasses are free to override
this method to print the contents of the entity instead.
EntityRef
to output.
Writer
to use.
protected void printCDATA(CDATA cdata, Writer out) throws IOException
This will handle printing of {@link CDATA}
text.
CDATA
to output.
Writer
to use.
protected void printText(Text text, Writer out) throws IOException
This will handle printing of {@link Text}
strings.
Text
to write.
Writer
to use.
protected void printString(String str, Writer out) throws IOException
protected void printElement(Element element, Writer out, int level, XMLOutputter. NamespaceStack namespaces) throws IOException
This will handle printing of a {@link Element}
,
it's {@link Attribute}
s, and all contained (child)
elements, etc.
Element
to output.
Writer
to use.
int
level of indention.
List
stack of Namespaces in scope.
protected void printElementContent(Element element, Writer out, int level, XMLOutputter. NamespaceStack namespaces) throws IOException
This will handle printing of a {@link
Element}
's content only, not including it's tag,
attributes, or namespace info.
Element
to output.
Writer
to use.
int
level of indentation.
List
stack of Namespaces in scope.
protected void printContent(List content, Writer out, int level, XMLOutputter. NamespaceStack namespaces) throws IOException
This will handle printing of a {@link
Element}
's content only, not including it's tag,
attributes, or namespace info.
List
of content to output.
Writer
to use.
int
level of indentation.
List
stack of Namespaces in scope.
protected void printContentRange(List content, int start, int end, Writer out, int level, XMLOutputter. NamespaceStack namespaces) throws IOException
This will handle printing of content within a given range. The range to print is specified in typical Java fashion; the starting index is inclusive, while the ending index is exclusive.
List
of content to output
Writer
to use.
int
level of indentation.
List
stack of Namespaces in scope.
protected void printTextRange(List content, int start, int end, Writer out) throws IOException
This will handle printing of a sequence of {@link CDATA}
or {@link Text}
nodes. It is a error to have any other
pass this method any other type of node.
List
of content to output
Writer
to use.
protected void printAttributes(List attributes, Element parent, Writer out, XMLOutputter. NamespaceStack namespaces) throws IOException
This will handle printing of a {@link Attribute}
list.
List
of Attribute objcts
Writer
to use
protected void newline(Writer out) throws IOException
This will print a new line only if the newlines flag was set to true.
Writer
to use
protected void indent(Writer out) throws IOException
This will print indents (only if the newlines flag was
set to true
, and indent is non-null).
Writer
to use
protected void indent(Writer out, int level) throws IOException
This will print indents (only if the newlines flag was
set to true
, and indent is non-null).
Writer
to use
public String escapeAttributeEntities(String 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. It does no converstion for ' because it's not necessary as the outputter writes attributes surrounded by double-quotes.
String
input to escape.
String
with escaped content.
public String escapeElementEntities(String 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.
String
input to escape.
String
with escaped content.
public int parseArgs(String args[], int i)
Parse command-line arguments of the form -omitEncoding
-indentSize 3 ...
public Object clone()
public String toString()
Return a string listing of the settings for this XMLOutputter instance.
protected XMLOutputter. NamespaceStack createNamespaceStack()
Factory for making new NamespaceStack objects. The NamespaceStack created is actually an inner class extending the package protected NamespaceStack, as a way to make NamespaceStack "friendly" toward subclassers.
public void setPadText(boolean padText)
Ensure that text immediately preceded by or followed by an element will be "padded" with a single space.
public void setIndentLevel(int level)
Set the initial indentation level.
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.
boolean
indicating whether or not
the XML declaration should be suppressed.
All Packages Class Hierarchy This Package Previous Next Index