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 constructor
creates an outputter to output the document exactly as created.
Constructor parameters control the indent amount and whether new lines
are printed between elements. The other parameters are configurable
through the set*
methods.
The XML declaration is always printed on its own line. Empty elements
are by default printed as <empty/> but that can be configured.
Text-only contents are printed as <tag>content</tag> on a
single line.
For compact machine-readable output create a default XMLOutputter and call setTextNormalize(true) to normalize any whitespace that was preserved from the source.
For pretty output, set the indent to " ", set the new lines feature to true, and set text trimming to true.
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 output(...)
and pass in your own Writer
or
OutputStream
to if possible.
This will create an XMLOutputter
with
no additional whitespace (indent or new lines) 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
.
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.
This will print the proper indent characters for the given indent level.
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 a
Print out a
This will print the This will print the
Print out an
Print out an
Print out an Print out an
Print out a
Print out a
Print out a Print out a
Print out a Print out a This will handle printing out an This will handle printing out an
This will handle printing out an
This will handle printing out an
This will write the comment to the specified writer.
This will write the declaration to the given Writer.
This will write the DOCTYPE declaration if one exists.
This will handle printing out an This will handle printing out an
This will handle printing out an
This will write the processing instruction to the specified writer.
This will set whether empty elements are expanded from
This will set the indent
This will set the indent This will set the new-line separator.
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 is output verbatim (false)
or with whitespace stripped.
Default: false {
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(ProcessingInstruction, OutputStream)
{
output(ProcessingInstruction, Writer)
{
output(String, OutputStream)
{
output(String, Writer)
{
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(ProcessingInstruction)
parseArgs(String[], int)
-omitEncoding
-indentSize 3 ...
printAttributes(List, Element, Writer, XMLOutputter. NamespaceStack)
{
printCDATA(CDATA, Writer)
{
printComment(Comment, Writer)
printDeclaration(Document, Writer, String)
printDocType(DocType, Writer)
printElement(Element, Writer, int, XMLOutputter. NamespaceStack)
{
printElementContent(Element, Writer, int, XMLOutputter. NamespaceStack, List)
{
printEntityRef(EntityRef, Writer)
{
printProcessingInstruction(ProcessingInstruction, Writer)
printString(String, Writer)
setEncoding(String)
setExpandEmptyElements(boolean)
<tagName>
to
<tagName></tagName>
.
setIndent(boolean)
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 space chars).
setLineSeparator(String)
setNewlines(boolean)
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)
{
setTrimText(boolean)
protected static final String STANDARD_INDENT
public XMLOutputter()
This will create an XMLOutputter
with
no additional whitespace (indent or new lines) 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 new-line separator. 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)
true
indicates new lines should be
printed, else new lines are ignored (compacted).
public void setEncoding(String 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 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 setTrimText(boolean textTrim)
This will set whether the text is output verbatim (false) or with whitespace stripped.
Default: false
boolean
true=>trim 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.
String
to use for indentation.
public void setIndent(boolean doIndent)
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 space chars).
int
number of spaces in indentation.
protected void indent(Writer out, int level) throws IOException
This will print the proper indent characters for the given indent level.
Writer
to write to
int
indentation level
protected void maybePrintln(Writer out) throws IOException
This will print a new line only if the newlines flag was set to true
Writer
to write to
protected Writer makeWriter(OutputStream out) throws UnsupportedEncodingException
protected Writer makeWriter(OutputStream out, String enc) throws UnsupportedEncodingException
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 write to.
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 write to.
public void output(Element element, Writer out) throws IOException
Print out an {@link Element}
, including
its {@link Attribute}
s, and its value, and all
contained (child) elements etc.
Element
to output.
Writer
to write to.
public void output(Element element, OutputStream out) throws IOException
Print out an {@link Element}
, including
its {@link Attribute}
s, and its value, and all
contained (child) elements etc.
Element
to output.
Writer
to write to.
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 write to.
public void outputElementContent(Element element, OutputStream 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.
OutputStream
to write to.
public void output(CDATA cdata, Writer out) throws IOException
Print out a {@link CDATA}
CDATA
to output.
Writer
to write to.
public void output(CDATA cdata, OutputStream out) throws IOException
Print out a {@link CDATA}
CDATA
to output.
OutputStream
to write to.
public void output(Comment comment, Writer out) throws IOException
Print out a {@link Comment}
Comment
to output.
Writer
to write to.
public void output(Comment comment, OutputStream out) throws IOException
Print out a {@link Comment}
Comment
to output.
OutputStream
to write to.
public void output(String string, Writer out) throws IOException
Print out a {@link java.lang.String}
. Perfoms
the necessary entity escaping and whitespace stripping.
String
to output.
Writer
to write to.
public void output(String string, OutputStream out) throws IOException
Print out a {@link java.lang.String}
. Perfoms
the necessary entity escaping and whitespace stripping.
String
to output.
OutputStream
to write to.
public void output(Text text, Writer out) throws IOException
Print out a {@link Text}
. Perfoms
the necessary entity escaping and whitespace stripping.
Text
to output.
Writer
to write to.
public void output(Text text, OutputStream out) throws IOException
Print out a {@link Text}
. Perfoms
the necessary entity escaping and whitespace stripping.
Text
to output.
OutputStream
to write to.
public void output(EntityRef entity, Writer out) throws IOException
Print out an {@link EntityRef}
.
EntityRef
to output.
Writer
to write to.
public void output(EntityRef entity, OutputStream out) throws IOException
Print out an {@link EntityRef}
.
EntityRef
to output.
OutputStream
to write to.
public void output(ProcessingInstruction pi, Writer out) throws IOException
Print out a {@link ProcessingInstruction}
ProcessingInstruction
to output.
Writer
to write to.
public void output(ProcessingInstruction pi, OutputStream out) throws IOException
Print out a {@link ProcessingInstruction}
ProcessingInstruction
to output.
OutputStream
to write to.
public void output(DocType doctype, Writer out) throws IOException
Print out a {@link DocType}
DocType
to output.
Writer
to write to.
public void output(DocType doctype, OutputStream out) throws IOException
Print out a {@link DocType}
DocType
to output.
OutputStream
to write to.
public String outputString(Document doc)
Document
to format.
public String outputString(Element element)
Element
to format.
public String outputString(Comment comment)
Comment
to format.
public String outputString(CDATA cdata)
CDATA
to format.
public String outputString(ProcessingInstruction pi)
ProcessingInstruction
to format.
public String outputString(DocType doctype)
DocType
to format.
public String outputString(EntityRef entity)
EntityRef
to format.
protected void printDeclaration(Document doc, Writer out, String encoding) throws IOException
This will write the declaration to the given Writer. Assumes XML version 1.0 since we don't directly know.
Document
whose declaration to write.
Writer
to write to.
protected void printDocType(DocType docType, Writer out) throws IOException
This will write the DOCTYPE declaration if one exists.
Document
whose declaration to write.
Writer
to write to.
protected void printComment(Comment comment, Writer out) throws IOException
This will write the comment to the specified writer.
Comment
to write.
Writer
to write to.
protected void printProcessingInstruction(ProcessingInstruction pi, Writer out) throws IOException
This will write the processing instruction to the specified writer.
ProcessingInstruction
to write.
Writer
to write to.
protected void printCDATA(CDATA cdata, Writer out) throws IOException
This will handle printing out an {@link CDATA}
,
and its value.
CDATA
to output.
Writer
to write to.
protected void printElement(Element element, Writer out, int indentLevel, XMLOutputter. NamespaceStack namespaces) throws IOException
This will handle printing out an {@link Element}
,
its {@link Attribute}
s, and its value.
Element
to output.
Writer
to write to.
int
level of indention.
List
stack of Namespaces in scope.
protected void printElementContent(Element element, Writer out, int indentLevel, XMLOutputter. NamespaceStack namespaces, List eltContent) throws IOException
This will handle printing out an {@link
Element}
's content only, not including its tag,
attributes, and namespace info.
Element
to output.
Writer
to write to.
int
level of indention.
protected void printString(String s, Writer out) throws IOException
protected void printEntityRef(EntityRef entity, Writer out) throws IOException
This will handle printing out an {@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 write to.
protected void printAttributes(List attributes, Element parent, Writer out, XMLOutputter. NamespaceStack namespaces) throws IOException
This will handle printing out an {@link Attribute}
list.
List
of Attribute objcts
Writer
to write to
protected String escapeAttributeEntities(String st)
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.
protected String escapeElementEntities(String st)
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.
String
input to escape.
String
with escaped content.
public int parseArgs(String args[], int i)
-omitEncoding
-indentSize 3 ...
protected XMLOutputter. NamespaceStack createNamespaceStack()
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 indentLevel)
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