org.jdom.output
public class XMLOutputter extends Object implements Cloneable
{@link Format#getPrettyFormat()}
. For whitespace-normalized
output, use {@link Format#getCompactFormat()}
.
There are {@link #output 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 Format object. 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 Format
manually in its constructor or via the
{@link Format#setEncoding}
method. The default encoding is
UTF-8.
The methods {@link #outputString outputString(...)}
are for
convenience only; for top performance you should call one of the {@link
#output output(...)}
methods 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
{@link Format#setOmitDeclaration}
. To omit printing of the
encoding in the declaration use {@link Format#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 {@link Format#setExpandEmptyElements}
to cause
them to be expanded to <empty></empty>.
Version: $Revision: 1.112 $, $Date: 2004/09/01 06:08:18 $
Nested Class Summary | |
---|---|
protected class | XMLOutputter.NamespaceStack
Our own null subclass of NamespaceStack. |
Field Summary | |
---|---|
protected Format | currentFormat |
protected static Format | preserveFormat |
Constructor Summary | |
---|---|
XMLOutputter()
This will create an XMLOutputter with the default
{@link Format} matching {@link Format#getRawFormat}. | |
XMLOutputter(Format format)
This will create an XMLOutputter with the specified
format characteristics. | |
XMLOutputter(XMLOutputter that)
This will create an XMLOutputter with all the
options as set in the given XMLOutputter . |
Method Summary | |
---|---|
Object | clone()
Returns a copy of this XMLOutputter. |
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. |
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.
|
Format | getFormat()
Returns the current format in use by the outputter. |
void | output(Document doc, OutputStream out)
This will print the Document to the given output stream.
|
void | output(DocType doctype, OutputStream out)
Print out the {@link DocType} .
|
void | output(Element element, OutputStream out)
Print out an {@link Element} , including
its {@link Attribute} s, and all
contained (child) elements, etc.
|
void | output(List list, OutputStream out)
This will handle printing out a list of nodes.
|
void | output(CDATA cdata, OutputStream out)
Print out a {@link CDATA} node.
|
void | output(Text text, OutputStream out)
Print out a {@link Text} node. |
void | output(Comment comment, OutputStream out)
Print out a {@link Comment} .
|
void | output(ProcessingInstruction pi, OutputStream out)
Print out a {@link ProcessingInstruction} .
|
void | output(EntityRef entity, OutputStream out)
Print out a {@link EntityRef} .
|
void | output(Document doc, Writer out)
This will print the Document to the given Writer.
|
void | output(DocType doctype, Writer out)
Print out the {@link DocType} .
|
void | output(Element element, Writer out)
Print out an {@link Element} , including
its {@link Attribute} s, and all
contained (child) elements, etc.
|
void | output(List list, Writer out)
This will handle printing out a list of nodes.
|
void | output(CDATA cdata, Writer out)
Print out a {@link CDATA} node.
|
void | output(Text text, Writer out)
Print out a {@link Text} node. |
void | output(Comment comment, Writer out)
Print out a {@link Comment} .
|
void | output(ProcessingInstruction pi, Writer out)
Print out a {@link ProcessingInstruction} .
|
void | output(EntityRef entity, Writer out)
Print out a {@link EntityRef} .
|
void | outputElementContent(Element element, OutputStream out)
This will handle printing out an {@link
Element} 's content only, not including its tag, and
attributes. |
void | outputElementContent(Element element, Writer out)
This will handle printing out an {@link
Element} 's content only, not including its tag, and
attributes. |
String | outputString(Document doc)
Return a string representing a document. |
String | outputString(DocType doctype)
Return a string representing a DocType. |
String | outputString(Element element)
Return a string representing an element. |
String | outputString(List list)
Return a string representing a list of nodes. |
String | outputString(CDATA cdata)
Return a string representing a CDATA node. |
String | outputString(Text text)
Return a string representing a Text node. |
String | outputString(Comment comment)
Return a string representing a comment. |
String | outputString(ProcessingInstruction pi)
Return a string representing a PI. |
String | outputString(EntityRef entity)
Return a string representing an entity. |
protected void | printAttributes(Writer out, List attributes, Element parent, XMLOutputter.NamespaceStack namespaces)
This will handle printing of a {@link Attribute} list.
|
protected void | printCDATA(Writer out, CDATA cdata)
This will handle printing of {@link CDATA} text.
|
protected void | printComment(Writer out, Comment comment)
This will handle printing of comments.
|
protected void | printDeclaration(Writer out, Document doc, String encoding)
This will handle printing of the declaration.
|
protected void | printDocType(Writer out, DocType docType)
This handle printing the DOCTYPE declaration if one exists.
|
protected void | printElement(Writer out, Element element, int level, XMLOutputter.NamespaceStack namespaces)
This will handle printing of a {@link Element} ,
its {@link Attribute} s, and all contained (child)
elements, etc.
|
protected void | printEntityRef(Writer out, EntityRef entity)
This will handle printing a {@link EntityRef} .
|
protected void | printProcessingInstruction(Writer out, ProcessingInstruction pi)
This will handle printing of processing instructions.
|
protected void | printText(Writer out, Text text)
This will handle printing of {@link Text} strings.
|
void | setFormat(Format newFormat)
Sets the new format logic for the outputter. |
String | toString()
Return a string listing of the settings for this
XMLOutputter instance.
|
XMLOutputter
with the default
{@link Format} matching {@link Format#getRawFormat}.XMLOutputter
with the specified
format characteristics. Note the format object is cloned internally
before use.XMLOutputter
with all the
options as set in the given XMLOutputter
. Note
that XMLOutputter two = (XMLOutputter)one.clone();
would work equally well.
Parameters: that the XMLOutputter to clone
Parameters: str String
input to escape.
Returns: String
with escaped content.
Parameters: str String
input to escape.
Returns: String
with escaped content.
Document
to the given output stream.
The characters are printed using the encoding specified in the
constructor, or a default of UTF-8.
Parameters: doc Document
to format. out OutputStream
to use.
Throws: IOException - if there's any problem writing.
{@link DocType}
.
Parameters: doctype DocType
to output. out OutputStream
to use.
{@link Element}
, including
its {@link Attribute}
s, and all
contained (child) elements, etc.
Parameters: element Element
to output. out Writer
to use.
Parameters: list List
of nodes. out OutputStream
to use.
{@link CDATA}
node.
Parameters: cdata CDATA
to output. out OutputStream
to use.
{@link Text}
node. Perfoms
the necessary entity escaping and whitespace stripping.
Parameters: text Text
to output. out OutputStream
to use.
{@link Comment}
.
Parameters: comment Comment
to output. out OutputStream
to use.
{@link ProcessingInstruction}
.
Parameters: pi ProcessingInstruction
to output. out OutputStream
to use.
{@link EntityRef}
.
Parameters: entity EntityRef
to output. out OutputStream
to use.
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.
Parameters: doc Document
to format. out Writer
to use.
Throws: IOException - if there's any problem writing.
{@link DocType}
.
Parameters: doctype DocType
to output. out Writer
to use.
{@link Element}
, including
its {@link Attribute}
s, and all
contained (child) elements, etc.
Parameters: element Element
to output. out Writer
to use.
Parameters: list List
of nodes. out Writer
to use.
{@link CDATA}
node.
Parameters: cdata CDATA
to output. out Writer
to use.
{@link Text}
node. Perfoms
the necessary entity escaping and whitespace stripping.
Parameters: text Text
to output. out Writer
to use.
{@link Comment}
.
Parameters: comment Comment
to output. out Writer
to use.
{@link ProcessingInstruction}
.
Parameters: pi ProcessingInstruction
to output. out Writer
to use.
{@link EntityRef}
.
Parameters: entity EntityRef
to output. out Writer
to use.
{@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>".
Parameters: element Element
to output. out OutputStream
to use.
{@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>".
Parameters: element Element
to output. out Writer
to use.
Parameters: doc Document
to format.
Parameters: doctype DocType
to format.
Parameters: element Element
to format.
Parameters: list List
to format.
Parameters: cdata CDATA
to format.
Parameters: text Text
to format.
Parameters: comment Comment
to format.
Parameters: pi ProcessingInstruction
to format.
Parameters: entity EntityRef
to format.
{@link Attribute}
list.
Parameters: attributes List
of Attribute objcts out Writer
to use
{@link CDATA}
text.
Parameters: cdata CDATA
to output. out Writer
to use.
Parameters: comment Comment
to write. out Writer
to use.
Parameters: doc Document
whose declaration to write. out Writer
to use. encoding The encoding to add to the declaration
Parameters: docType Document
whose declaration to write. out Writer
to use.
{@link Element}
,
its {@link Attribute}
s, and all contained (child)
elements, etc.
Parameters: element Element
to output. out Writer
to use. level int
level of indention. namespaces List
stack of Namespaces in scope.
{@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.
Parameters: entity EntityRef
to output. out Writer
to use.
Parameters: pi ProcessingInstruction
to write. out Writer
to use.
{@link Text}
strings.
Parameters: text Text
to write. out Writer
to use.
Parameters: newFormat the format to use for output
Returns: a string listing the settings for this XMLOutputter instance