getRawFormat (no whitespace changes),
getPrettyFormat (whitespace beautification), and
getCompactFormat (whitespace normalization).
Several modes are available to effect the way textual content is printed.
See the documentation for TextMode for details.
Note about Line Separator:
By default JDOM will always use the CRNL sequence "\r\n" for output. This
can be changed in a number of different ways. See the LineSeparator
enumeration for more information.
Note about XML Character Escaping:
JDOM will escape characters in the output based on the EscapeStrategy that is specified by this Format. The Format will by default use a sensible EscapeStrategy that is based on the character encoding of the output. If the default escape mechanism is not producing the correct results you can change the EscapeStrategy on the format to suit your own needs.
LineSeparator
| Modifier and Type | Class and Description |
|---|---|
| private static class | Format.DefaultCharsetEscapeStrategy
Handles Charsets. |
| private static class | Format.EscapeStrategy7Bits
An EscapeStrategy suitable for 7-bit charsets. |
| private static class | Format.EscapeStrategy8Bits
An EscapeStrategy suitable for 8-bit charsets. |
| private static class | Format.EscapeStrategyUTF
An EscapeStrategy suitable for UTF-8 an UTF-16. |
| public static enum | Format.TextMode
Class to signify how text should be handled on output. |
| Modifier and Type | Field and Description |
|---|---|
| private static final EscapeStrategy | Bits7EscapeStrategy
An EscapeStrategy suitable for 7-bit charsets |
| private static final EscapeStrategy | Bits8EscapeStrategy
An EscapeStrategy suitable for 8-bit charsets |
| private static final EscapeStrategy | DefaultEscapeStrategy
An EscapeStrategy suitable for 'unknown' charsets |
| pack-priv String | encoding
The encoding format |
| pack-priv EscapeStrategy | escapeStrategy
entity escape logic |
| pack-priv boolean | expandEmptyElements
Whether or not to expand empty elements to
<tagName></tagName> - default is |
| pack-priv boolean | ignoreTrAXEscapingPIs
Whether TrAX output escaping disabling/enabling PIs are ignored
or processed - default is |
| pack-priv String | indent
The default indent is no spaces (as original document) |
| pack-priv String | lineSeparator
New line separator |
| pack-priv Format. | mode
text handling mode |
| pack-priv boolean | omitDeclaration
Whether or not to output the XML declaration
- default is |
| pack-priv boolean | omitEncoding
Whether or not to output the encoding in the XML declaration
- default is |
| pack-priv boolean | specifiedAttributesOnly
Whether Attributes that are defaulted from the DTD or Schema are output. |
| private static final String | STANDARD_ENCODING
standard encoding |
| private static final String | STANDARD_INDENT
standard value to indent by, if we are indenting |
| private static final String | STANDARD_LINE_SEPARATOR
standard string with which to end a line |
| private static final EscapeStrategy | UTFEscapeStrategy
An EscapeStrategy suitable for UTF-8 an UTF-16 |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static final EscapeStrategy | |
| public Format | |
| public static final String | |
| public static final String | Returns: The value appropriately escaped.The EscapeStrategy to query. strategy, final String String Attribute value to escape.This will take the three pre-defined entities in XML 1.0 ('<', '>', and '&' - used specifically in XML elements) as well as CR/NL, tabs, and Quote characters which require escaping inside Attribute values and converts their character representation to the appropriate entity reference suitable for XML attribute content. |
| public static final String | Returns: The input value escaped.The EscapeStrategy strategy, final String The End-Of-Line sequence to be used (may be null). eol, final String The String to escape value)This will take the three pre-defined entities in XML 1.0 ('<', '>', and '&' - used specifically in XML elements) and convert their character representation to the appropriate entity reference, suitable for XML element content. |
| public static Format | Returns: a Format with whitespace normalizationReturns a new Format object that performs whitespace normalization, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy. |
| public String | |
| public EscapeStrategy | |
| public boolean | Returns: whether empty elements are expandedReturns whether empty elements are expanded. |
| public boolean | Returns: whether or not TrAX output escaping PIs are ignored.Returns whether JAXP TrAX processing instructions for disabling/enabling output escaping are ignored. |
| public String | |
| public String | |
| public boolean | Returns: whether the XML declaration will be omittedReturns whether the XML declaration will be omitted. |
| public boolean | Returns: whether the XML declaration encoding will be omittedReturns whether the XML declaration encoding will be omitted. |
| public static Format | Returns: a Format with whitespace beautificationReturns a new Format object that performs whitespace beautification with 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy. |
| public static Format | Returns: a Format with no whitespace changesReturns a new Format object that performs no whitespace changes, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy. |
| public Format. | |
| public boolean | Returns: true if the defaulted Attributes will be outputWill Attributes defaulted from the DTD or XMLSchema be output |
| public Format | Returns: a pointer to this Format for chainingthe encoding format. Use XML-style names like
"UTF-8" or "ISO-8859-1" or "US-ASCII" encoding)Sets the output encoding. |
| public Format | Returns: a pointer to this Format for chainingthe EscapeStrategy to use strategy)Sets the |
| public Format | Returns: a pointer to this Format for chainingboolean indicating whether or not
empty elements should be expanded.This will set whether empty elements are expanded from
|
| public void | setIgnoreTrAXEscapingPIs(boolean
boolean indicating
whether or not TrAX output escaping PIs are ignored.This will set whether JAXP TrAX processing instructions for disabling/enabling output escaping are ignored. |
| public Format | |
| public Format | Returns: a pointer to this Format for chainingString line separator to use.This will set the newline separator ( |
| public Format | Returns: a pointer to this Format for chainingLineSeparator line separator to usThis will set the newline separator sequence. |
| public Format | Returns: a pointer to this Format for chainingboolean indicating whether or not
the XML declaration should be omitted.This will set whether the XML declaration
( |
| public Format | Returns: a pointer to this Format for chainingboolean indicating whether or not
the XML declaration should indicate the document encoding.This will set whether the XML declaration
( |
| public void | setSpecifiedAttributesOnly(boolean
true if the defaulted
Attributes should be ignored, false if they should be output specifiedAttributesOnly)Set whether only those Attributes specified in the input XML should be output. |
| public Format | Returns: a pointer to this Format for chainingThe TextMode to set. modeThis sets the text output style. |
| public static final String | |
| public static final String | |
| public static final String |
| Bits7EscapeStrategy | back to summary |
|---|---|
| private static final EscapeStrategy Bits7EscapeStrategy An EscapeStrategy suitable for 7-bit charsets | |
| Bits8EscapeStrategy | back to summary |
|---|---|
| private static final EscapeStrategy Bits8EscapeStrategy An EscapeStrategy suitable for 8-bit charsets | |
| DefaultEscapeStrategy | back to summary |
|---|---|
| private static final EscapeStrategy DefaultEscapeStrategy An EscapeStrategy suitable for 'unknown' charsets | |
| encoding | back to summary |
|---|---|
| pack-priv String encoding The encoding format | |
| escapeStrategy | back to summary |
|---|---|
| pack-priv EscapeStrategy escapeStrategy entity escape logic | |
| expandEmptyElements | back to summary |
|---|---|
| pack-priv boolean expandEmptyElements Whether or not to expand empty elements to
<tagName></tagName> - default is | |
| ignoreTrAXEscapingPIs | back to summary |
|---|---|
| pack-priv boolean ignoreTrAXEscapingPIs Whether TrAX output escaping disabling/enabling PIs are ignored
or processed - default is | |
| indent | back to summary |
|---|---|
| pack-priv String indent The default indent is no spaces (as original document) | |
| lineSeparator | back to summary |
|---|---|
| pack-priv String lineSeparator New line separator | |
| mode | back to summary |
|---|---|
| pack-priv Format. text handling mode | |
| omitDeclaration | back to summary |
|---|---|
| pack-priv boolean omitDeclaration Whether or not to output the XML declaration
- default is | |
| omitEncoding | back to summary |
|---|---|
| pack-priv boolean omitEncoding Whether or not to output the encoding in the XML declaration
- default is | |
| specifiedAttributesOnly | back to summary |
|---|---|
| pack-priv boolean specifiedAttributesOnly Whether Attributes that are defaulted from the DTD or Schema are output. | |
| STANDARD_ENCODING | back to summary |
|---|---|
| private static final String STANDARD_ENCODING standard encoding | |
| STANDARD_INDENT | back to summary |
|---|---|
| private static final String STANDARD_INDENT standard value to indent by, if we are indenting | |
| STANDARD_LINE_SEPARATOR | back to summary |
|---|---|
| private static final String STANDARD_LINE_SEPARATOR standard string with which to end a line | |
| UTFEscapeStrategy | back to summary |
|---|---|
| private static final EscapeStrategy UTFEscapeStrategy An EscapeStrategy suitable for UTF-8 an UTF-16 | |
| Format | back to summary |
|---|---|
| private Format() Creates a new Format instance with default (raw) behavior. | |
| chooseStrategy | back to summary |
|---|---|
| private static final EscapeStrategy chooseStrategy(String encoding) | |
| clone | back to summary |
|---|---|
| public Format clone() Overrides java. Doc from java. Creates and returns a copy of this object. The precise meaning
of "copy" may depend on the class of the object. The general
intent is that, for any object will be true, and that the expression:x.clone() != x will bex.clone().getClass() == x.getClass() true, but these are not absolute requirements.
While it is typically the case that:
will bex.clone().equals(x) true, this is not an absolute requirement.
By convention, the returned object should be obtained by calling
By convention, the object returned by this method should be independent
of this object (which is being cloned). To achieve this independence,
it may be necessary to modify one or more fields of the object returned
by | |
| compact | back to summary |
|---|---|
| public static final String compact(String str) Use the XML Specification definition of whitespace to compact the input value. The value is trimmed, and any internal XML whitespace is replaced with a single ' ' space. | |
| escapeAttribute | back to summary |
|---|---|
| public static final String escapeAttribute(final EscapeStrategy strategy, final String value) This will take the three pre-defined entities in XML 1.0 ('<', '>', and '&' - used specifically in XML elements) as well as CR/NL, tabs, and Quote characters which require escaping inside Attribute values and converts their character representation to the appropriate entity reference suitable for XML attribute content. Further, some special characters (e.g. characters that are not valid in the current encoding) are converted to escaped representations.
| |
| escapeText | back to summary |
|---|---|
| public static final String escapeText(final EscapeStrategy strategy, final String eol, final String value) This will take the three pre-defined entities in XML 1.0 ('<', '>', and '&' - used specifically in XML elements) and convert their character representation to the appropriate entity reference, suitable for XML element content. Further, some special characters (e.g. characters that are not valid in the current encoding) are converted to escaped representations. If the eol parameter is not null, then any internal newlines will be replaced with the specified eol sequence.
| |
| getCompactFormat | back to summary |
|---|---|
| public static Format getCompactFormat() Returns a new Format object that performs whitespace normalization, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy. Tweaks can be made to the returned Format instance without affecting other instances.
| |
| getEncoding | back to summary |
|---|---|
| public String getEncoding() Returns the configured output encoding.
| |
| getEscapeStrategy | back to summary |
|---|---|
| public EscapeStrategy getEscapeStrategy() Returns the current escape strategy
| |
| getExpandEmptyElements | back to summary |
|---|---|
| public boolean getExpandEmptyElements() Returns whether empty elements are expanded.
| |
| getIgnoreTrAXEscapingPIs | back to summary |
|---|---|
| public boolean getIgnoreTrAXEscapingPIs() Returns whether JAXP TrAX processing instructions for disabling/enabling output escaping are ignored.
| |
| getIndent | back to summary |
|---|---|
| public String getIndent() Returns the indent string in use.
| |
| getLineSeparator | back to summary |
|---|---|
| public String getLineSeparator() Returns the current line separator.
| |
| getOmitDeclaration | back to summary |
|---|---|
| public boolean getOmitDeclaration() Returns whether the XML declaration will be omitted.
| |
| getOmitEncoding | back to summary |
|---|---|
| public boolean getOmitEncoding() Returns whether the XML declaration encoding will be omitted.
| |
| getPrettyFormat | back to summary |
|---|---|
| public static Format getPrettyFormat() Returns a new Format object that performs whitespace beautification with 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy. Tweaks can be made to the returned Format instance without affecting other instances.
| |
| getRawFormat | back to summary |
|---|---|
| public static Format getRawFormat() Returns a new Format object that performs no whitespace changes, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy. Tweaks can be made to the returned Format instance without affecting other instances.
| |
| getTextMode | back to summary |
|---|---|
| public Format. Returns the current text output style. | |
| isSpecifiedAttributesOnly | back to summary |
|---|---|
| public boolean isSpecifiedAttributesOnly() Will Attributes defaulted from the DTD or XMLSchema be output
| |
| setEncoding | back to summary |
|---|---|
| public Format setEncoding(String encoding) Sets the output encoding. The name should be an accepted XML encoding. | |
| setEscapeStrategy | back to summary |
|---|---|
| public Format setEscapeStrategy(EscapeStrategy strategy) Sets the
| |
| setExpandEmptyElements | back to summary |
|---|---|
| public Format setExpandEmptyElements(boolean expandEmptyElements) This will set whether empty elements are expanded from
| |
| setIgnoreTrAXEscapingPIs | back to summary |
|---|---|
| public void setIgnoreTrAXEscapingPIs(boolean ignoreTrAXEscapingPIs) This will set whether JAXP TrAX processing instructions for disabling/enabling output escaping are ignored. Disabling output escaping allows using XML text as element content and outputting it verbatim, i.e. as element children would be.
When processed, these processing instructions are removed from
the generated XML text and control whether the element text
content is output verbatim or with escaping of the pre-defined
entities in XML 1.0. The text to be output verbatim shall be
surrounded by the
When ignored, the processing instructions are present in the generated XML text and the pre-defined entities in XML 1.0 are escaped.
Default:
| |
| setIndent | back to summary |
|---|---|
| public Format setIndent(String indent) This will set the indent | |
| setLineSeparator | back to summary |
|---|---|
| public Format setLineSeparator(String separator) This will set the newline separator (
Use the
To make it output the system default line ending string, call
To output "UNIX-style" documents, call
Note that this only applies to newlines generated by the
outputter. All XML parsers are required to 'normalize' all the
combinations of line separators to just '\n'. As a result, if any JDOM
component has an end-of-line-like value (e.g. '\r') in it then that value
must be the result of an escaped value in the XML source document
The actual newline separator itself though can be set with this method. Any internal newlines in Text output will be represented by this end-of-line sequence. For example, the following code:
Text txt = new Text("\r\n");
XMLOutputter xout = new XMLOutputter();
String result = xout.outputString(txt);
will produce the literal String sequence "
\r\n" because the
original \r is escaped to be 
 and the original \n
is replaced with the JDOM default Line Separator "\r\n".
If the format's "indent" property is null (as is the default for the Raw and Compact formats), then this value only effects the newlines written after the declaration and doctype, as well as any newlines embedded within existing text content. Setting the indent to be null will disable end-of-line processing for any formatting, but will not affect substitution of embedded \n. Setting this value to null or the empty string will disable all end-of-line modifications.
| |
| setLineSeparator | back to summary |
|---|---|
| public Format setLineSeparator(LineSeparator separator) This will set the newline separator sequence.
This method differs from Format.setLinewSeparator(LineSeparator.NONE);
| |
| setOmitDeclaration | back to summary |
|---|---|
| public Format setOmitDeclaration(boolean omitDeclaration) This will set whether the XML declaration
(
| |
| setOmitEncoding | back to summary |
|---|---|
| public Format setOmitEncoding(boolean omitEncoding) This will set whether the XML declaration
(
| |
| setSpecifiedAttributesOnly | back to summary |
|---|---|
| public void setSpecifiedAttributesOnly(boolean specifiedAttributesOnly) Set whether only those Attributes specified in the input XML should be output. Other Attributes (those defaulted or 'fixed' in the DTD or XMLSchema) should be ignored.
| |
| setTextMode | back to summary |
|---|---|
| public Format setTextMode(Format. This sets the text output style. Options are available as static
| |
| trimBoth | back to summary |
|---|---|
| public static final String trimBoth(final String str) Use the XML Specification definition of whitespace to trim the input value. | |
| trimLeft | back to summary |
|---|---|
| public static final String trimLeft(final String str) Use the XML Specification definition of whitespace to Left-trim the input value. | |
| trimRight | back to summary |
|---|---|
| public static final String trimRight(String str) Use the XML Specification definition of whitespace to Right-trim the input value. | |
| Modifier and Type | Field and Description |
|---|---|
| private final CharsetEncoder |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | shouldEscape(final char
The char to test to determine whether it should be escaped. ch)Implements org. |
| encoder | back to summary |
|---|---|
| private final CharsetEncoder encoder | |
| DefaultCharsetEscapeStrategy | back to summary |
|---|---|
| public DefaultCharsetEscapeStrategy(CharsetEncoder cse) | |
| shouldEscape | back to summary |
|---|---|
| public boolean shouldEscape(final char ch) Implements org. Doc from org. Test whether the supplied character should be formatted literally or as a character entity.
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | shouldEscape(final char
The char to test to determine whether it should be escaped. ch)Implements org. |
| EscapeStrategy7Bits | back to summary |
|---|---|
| private EscapeStrategy7Bits() | |
| shouldEscape | back to summary |
|---|---|
| public boolean shouldEscape(final char ch) Implements org. Doc from org. Test whether the supplied character should be formatted literally or as a character entity.
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | shouldEscape(final char
The char to test to determine whether it should be escaped. ch)Implements org. |
| EscapeStrategy8Bits | back to summary |
|---|---|
| private EscapeStrategy8Bits() | |
| shouldEscape | back to summary |
|---|---|
| public boolean shouldEscape(final char ch) Implements org. Doc from org. Test whether the supplied character should be formatted literally or as a character entity.
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public final boolean | shouldEscape(char
The char to test to determine whether it should be escaped. ch)Implements org. |
| EscapeStrategyUTF | back to summary |
|---|---|
| private EscapeStrategyUTF() | |
| shouldEscape | back to summary |
|---|---|
| public final boolean shouldEscape(char ch) Implements org. Doc from org. Test whether the supplied character should be formatted literally or as a character entity.
| |
| Text Mode | Resulting behavior. |
|---|---|
| PRESERVE (Default) | All content is printed in the format it was created, no whitespace or line separators are are added or removed. |
| TRIM_FULL_WHITE | Content between tags consisting of all whitespace is not printed. If the content contains even one non-whitespace character, it is all printed verbatim, whitespace and all. |
| TRIM | All leading and trailing whitespace is trimmed. |
| NORMALIZE | Leading and trailing whitespace is trimmed, and any 'internal' whitespace is compressed to a single space. |
When an element has a xml:space attribute with the value of "preserve",
all formatting is turned off (actually, the TextMode is set to
PRESERVE until the element and its 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.
| Modifier and Type | Field and Description |
|---|---|
| public static final Format. | NORMALIZE
Mode for text normalization (left and right trim plus internal whitespace is normalized to a single space. |
| public static final Format. | PRESERVE
Mode for literal text preservation. |
| public static final Format. | TRIM
Mode for text trimming (left and right trim). |
| public static final Format. | TRIM_FULL_WHITE
Mode for text trimming of content consisting of nothing but whitespace but otherwise not changing output. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static Format. | |
| public static Format. |
| NORMALIZE | back to summary |
|---|---|
| public static final Format. Mode for text normalization (left and right trim plus internal whitespace is normalized to a single space.
| |
| PRESERVE | back to summary |
|---|---|
| public static final Format. Mode for literal text preservation. | |
| TRIM | back to summary |
|---|---|
| public static final Format. Mode for text trimming (left and right trim). | |
| TRIM_FULL_WHITE | back to summary |
|---|---|
| public static final Format. Mode for text trimming of content consisting of nothing but whitespace but otherwise not changing output. | |
| TextMode | back to summary |
|---|---|
| private TextMode() | |
| valueOf | back to summary |
|---|---|
| public static Format. | |
| values | back to summary |
|---|---|
| public static Format. | |