| |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
XMLOutputter
takes a JDOM tree and formats it to a
stream as XML. This formatter performs typical document
formatting. The XML declaration and processing instructions are
always on their own lines. Empty elements are printed as
<empty/> and text-only contents are printed as
<tag>content</tag> on a single line. Constructor
parameters control the indent amount and whether new lines are
printed between elements. The other parameters are configurable
through the set*
methods.
For compact machine-readable output create a default XMLOutputter and call setTrimText(true) to strip any whitespace that was preserved from the source.
There are output(...)
methods to print any of the
standard JDOM classes, including Document
and
Element
, to either a Writer
or an
OutputStream
. Warning: using your own
Writer
may cause the outputter's preferred character
encoding to be ignored. If you use encodings other than UTF8, we
recommend using the method that takes an OutputStream instead.
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.
Constructor Summary | |
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. | |
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. | |
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. | |
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 . | |
XMLOutputter(XMLOutputter that) This will create an XMLOutputter with all the
options as set in the given XMLOutputter . |
Method Summary | |
void | output(Document doc, OutputStream out) This will print the Document to the given output stream. |
void | output(Document doc, Writer writer) This will print the Document to the given
Writer. |
void | output(Element element, Writer out) Print out an Element , including
its Attribute s, and its value, and all
contained (child) elements etc. |
void | output(Element element, OutputStream out) Print out an Element , including
its Attribute s, and its value, and all
contained (child) elements etc. |
void | output(CDATA cdata, Writer out) Print out a CDATA
|
void | output(CDATA cdata, OutputStream out) Print out a CDATA
|
void | output(Comment comment, Writer out) Print out a Comment
|
void | output(Comment comment, OutputStream out) Print out a Comment
|
void | output(String string, Writer out) Print out a String . |
void | output(String string, OutputStream out) Print out a String . |
void | output(Entity entity, Writer out) Print out an Entity . |
void | output(Entity entity, OutputStream out) Print out an Entity . |
void | output(ProcessingInstruction pi, Writer out) Print out a ProcessingInstruction
|
void | output(ProcessingInstruction pi, OutputStream out) Print out a ProcessingInstruction
|
void | outputElementContent(Element element, Writer out) This will handle printing out an Element 's content only, not including its tag, and
attributes. |
String | outputString(Document doc) Return a string representing a document. |
String | outputString(Element element) Return a string representing an element. |
int | parseArgs(String[] args, int i) parse command-line arguments of the form -omitEncoding
-indentSize 3 ... |
void | setEncoding(String encoding) |
void | setExpandEmptyElements(boolean expandEmptyElements) This will set whether empty elements are expanded from <tagName> to
<tagName></tagName> . |
void | setIndent(String indent) This will set the indent String to use; this
is usually a String of empty spaces. |
void | setIndent(boolean doIndent) Set the indent on or off. |
void | setIndentLevel(int indentLevel) Set the initial indentation level. |
void | setIndentSize(int indentSize) This will set the indent String 's size; an indentSize
of 4 would result in the indention being equivalent to the
String " " (four space chars). |
void | setLineSeparator(String separator) This will set the new-line separator. |
void | setNewlines(boolean newlines) |
void | setOmitEncoding(boolean omitEncoding) This will set whether the XML declaration ( <?xml version="1.0" encoding="UTF-8"?> )
includes the encoding of the document. |
void | setPadText(boolean padText) Ensure that text immediately preceded by or followed by an element will be "padded" with a single space. |
void | setSuppressDeclaration(boolean suppressDeclaration) This will set whether the XML declaration ( <?xml version="1.0"?> )
will be suppressed or not. |
void | setTrimText(boolean trimText) This will set whether the text is output verbatim (false) or with whitespace stripped as per org.jdom.Element.getTextTrim() . |
Constructor Detail |
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 spacestrue
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 spacestrue
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.
Method Detail |
public void output(Document doc, OutputStream out)
throws java.io.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.IOException
- - if there's any problem writing.public void output(Document doc, Writer writer)
throws java.io.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 UTF8, we recommend using the method that takes an OutputStream instead.
Note: as with all Writers, you may need to flush() yours after this method returns.
Document
to format.Writer
to write to.IOException
- - if there's any problem writing.public void output(Element element, Writer out)
throws java.io.IOException
Print out an Element
, including
its 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 java.io.IOException
Print out an Element
, including
its Attribute
s, and its value, and all
contained (child) elements etc.
Element
to output.Writer
to write to.public void output(CDATA cdata, Writer out)
throws java.io.IOException
Print out a CDATA
CDATA
to output.Writer
to write to.public void output(CDATA cdata, OutputStream out)
throws java.io.IOException
Print out a CDATA
CDATA
to output.OutputStream
to write to.public void output(Comment comment, Writer out)
throws java.io.IOException
Print out a Comment
Comment
to output.Writer
to write to.public void output(Comment comment, OutputStream out)
throws java.io.IOException
Print out a Comment
Comment
to output.OutputStream
to write to.public void output(String string, Writer out)
throws java.io.IOException
Print out a String
. Perfoms
the necessary entity escaping and whitespace stripping.
String
to output.Writer
to write to.public void output(String string, OutputStream out)
throws java.io.IOException
Print out a String
. Perfoms
the necessary entity escaping and whitespace stripping.
CDATA
to output.OutputStream
to write to.public void output(Entity entity, Writer out)
throws java.io.IOException
Print out an Entity
.
Entity
to output.Writer
to write to.public void output(Entity entity, OutputStream out)
throws java.io.IOException
Print out an Entity
.
CDATA
to output.OutputStream
to write to.public void output(ProcessingInstruction pi, Writer out)
throws java.io.IOException
Print out a ProcessingInstruction
ProcessingInstruction
to output.Writer
to write to.public void output(ProcessingInstruction pi, OutputStream out)
throws java.io.IOException
Print out a ProcessingInstruction
ProcessingInstruction
to output.OutputStream
to write to.public void outputElementContent(Element element, Writer out)
throws java.io.IOException
This will handle printing out an 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.int
level of indention.public String outputString(Document doc)
throws java.io.IOException
Document
to format.public String outputString(Element element)
throws java.io.IOException
Element
to format.public int parseArgs(String[] args, int i)
-omitEncoding
-indentSize 3 ...
public void setEncoding(String encoding)
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 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 setIndentLevel(int indentLevel)
public void setIndentSize(int indentSize)
This will set the indent String
's size; an indentSize
of 4 would result in the indention being equivalent to the
String
" " (four space chars).
int
number of spaces in indentation.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"))
We could change this to the System default, but I prefer not to make output platform dependent. A carriage return, linefeed pair is the most generally acceptable linebreak. Another possibility is to use only a line feed, which is XML's preferred (but not required) solution. However, both carriage return and linefeed are required for many network protocols, and the parser on the other end should normalize this. --Rusty
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 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 suppress
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 setPadText(boolean padText)
Ensure that text immediately preceded by or followed by an
element will be "padded" with a single space. This is used to
allow make browser-friendly HTML, avoiding trimText's
transformation of, e.g.,
The quick <b>brown</b> fox
into
The quick<b>brown</b>fox
(the latter
will run the three separate words together into a single word).
This setting is not too useful if you haven't also called
setTrimText(boolean).
Default: false
boolean
if true, pad string-element
boundariespublic 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.public void setTrimText(boolean trimText)
This will set whether the text is output verbatim (false)
or with whitespace stripped as per org.jdom.Element.getTextTrim()
.
Default: false
boolean
true=>trim the whitespace,
false=>use text verbatimAssociation Links |
to Class java.lang.String
to Class java.lang.String
to Class java.lang.String
to Class java.lang.String
| |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |