|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.jdom.output.XMLOutputter
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.
Field Summary | |
protected static java.lang.String |
STANDARD_INDENT
standard value to indent by, if we are indenting |
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(java.lang.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(java.lang.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(java.lang.String indent,
boolean newlines,
java.lang.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 | |
protected void |
indent(java.io.Writer out,
int level)
This will print the proper indent characters for the given indent level. |
protected java.io.Writer |
makeWriter(java.io.OutputStream out)
Get an OutputStreamWriter, use preferred encoding. |
protected java.io.Writer |
makeWriter(java.io.OutputStream out,
java.lang.String encoding)
Get an OutputStreamWriter, use specified encoding. |
protected void |
maybePrintln(java.io.Writer out)
This will print a new line only if the newlines flag was set to true |
void |
output(CDATA cdata,
java.io.OutputStream out)
Print out a
|
void |
output(CDATA cdata,
java.io.Writer out)
Print out a
|
void |
output(Comment comment,
java.io.OutputStream out)
Print out a
|
void |
output(Comment comment,
java.io.Writer out)
Print out a
|
void |
output(Document doc,
java.io.OutputStream out)
This will print the Document to the given output stream. |
void |
output(Document doc,
java.io.Writer writer)
This will print the Document to the given
Writer. |
void |
output(Element element,
java.io.OutputStream out)
Print out an , including
its s, and its value, and all
contained (child) elements etc. |
void |
output(Element element,
java.io.Writer out)
Print out an , including
its s, and its value, and all
contained (child) elements etc. |
void |
output(Entity entity,
java.io.OutputStream out)
Print out an . |
void |
output(Entity entity,
java.io.Writer out)
Print out an . |
void |
output(ProcessingInstruction processingInstruction,
java.io.OutputStream out)
Print out a
|
void |
output(ProcessingInstruction processingInstruction,
java.io.Writer out)
Print out a
|
void |
output(java.lang.String string,
java.io.OutputStream out)
Print out a . |
void |
output(java.lang.String string,
java.io.Writer out)
Print out a . |
java.lang.String |
outputString(Document doc)
Return a string representing a document. |
java.lang.String |
outputString(Element element)
Return a string representing an element. |
int |
parseArgs(java.lang.String[] args,
int i)
parse command-line arguments of the form -omitEncoding
-indentSize 3 ... |
protected void |
printAttributes(java.util.List attributes,
Element parent,
java.io.Writer out,
org.jdom.output.NamespaceStack namespaces)
This will handle printing out an list. |
protected void |
printCDATASection(CDATA cdata,
java.io.Writer out,
int indentLevel)
This will handle printing out an ,
and its value. |
protected void |
printComment(Comment comment,
java.io.Writer out,
int indentLevel)
This will write the comment to the specified writer. |
void |
printDeclaration(Document doc,
java.io.Writer out,
java.lang.String encoding)
This will write the declaration to the given Writer. |
void |
printDocType(DocType docType,
java.io.Writer out)
This will write the DOCTYPE declaration if one exists. |
protected void |
printElement(Element element,
java.io.Writer out,
int indentLevel,
org.jdom.output.NamespaceStack namespaces)
This will handle printing out an ,
its s, and its value. |
void |
printElementContent(Element element,
java.io.Writer out)
This will handle printing out an 's content only, not including its tag,
attributes, and namespace info. |
protected void |
printElementContent(Element element,
java.io.Writer out,
int indentLevel,
org.jdom.output.NamespaceStack namespaces,
java.util.List mixedContent)
This will handle printing out an 's content only, not including its tag,
attributes, and namespace info. |
protected void |
printEntity(Entity entity,
java.io.Writer out)
This will handle printing out an . |
protected void |
printNamespace(Namespace ns,
java.io.Writer out)
This will handle printing out any needed
declarations. |
protected void |
printProcessingInstruction(ProcessingInstruction pi,
java.io.Writer out,
int indentLevel)
This will write the processing instruction to the specified writer. |
protected void |
printString(java.lang.String s,
java.io.Writer out)
Print a string. |
void |
setEncoding(java.lang.String encoding)
|
void |
setExpandEmptyElements(boolean expandEmptyElements)
This will set whether empty elements are expanded from <tagName> to
<tagName></tagName> . |
void |
setIndent(boolean doIndent)
Set the indent on or off. |
void |
setIndent(java.lang.String indent)
This will set the indent String to use; this
is usually a String of empty spaces. |
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 characters). |
void |
setLineSeparator(java.lang.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 |
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 .
Default: false |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Field Detail |
protected static final java.lang.String STANDARD_INDENT
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(java.lang.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.
indent
- the indent string, usually some number of spacespublic XMLOutputter(java.lang.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.
indent
- the indent String
, usually some number
of spacesnewlines
- true
indicates new lines should be
printed, else new lines are ignored (compacted).public XMLOutputter(java.lang.String indent, boolean newlines, java.lang.String encoding)
This will create an XMLOutputter
with
the given indent and new lines printing only if newlines is
true
, and encoding format encoding
.
indent
- the indent String
, usually some number
of spacesnewlines
- true
indicates new lines should be
printed, else new lines are ignored (compacted).encoding
- set encoding format.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.
that
- the XMLOutputter to cloneMethod Detail |
public void setLineSeparator(java.lang.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.
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
separator
- String
line separator to use.setNewlines(boolean)
public void setNewlines(boolean newlines)
newlines
- true
indicates new lines should be
printed, else new lines are ignored (compacted).setLineSeparator(String)
public void setEncoding(java.lang.String encoding)
encoding
- encoding formatpublic 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.
omitEncoding
- boolean
indicating whether or not
the XML declaration should indicate the document encoding.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.
suppressDeclaration
- boolean
indicating whether or not
the XML declaration should be suppressed.public void setExpandEmptyElements(boolean expandEmptyElements)
This will set whether empty elements are expanded from <tagName>
to
<tagName></tagName>
.
expandEmptyElements
- boolean
indicating whether or not
empty elements should be expanded.public void setTrimText(boolean trimText)
This will set whether the text is output verbatim (false)
or with whitespace stripped as per
.Element.getTextTrim()
Default: false
trimText
- boolean
true=>trim the whitespace, false=>use text verbatimpublic void setIndent(java.lang.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.
indent
- String
to use for indentation.public void setIndent(boolean doIndent)
doIndent
- if true, set indenting on; if false, set indenting offpublic void setIndentLevel(int indentLevel)
indentLevel
- the number of indents to start withpublic 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 characters).
indentSize
- int
number of spaces in indentation.protected void indent(java.io.Writer out, int level) throws java.io.IOException
This will print the proper indent characters for the given indent level.
out
- Writer
to write tolevel
- int
indentation levelprotected void maybePrintln(java.io.Writer out) throws java.io.IOException
This will print a new line only if the newlines flag was set to true
out
- Writer
to write toprotected java.io.Writer makeWriter(java.io.OutputStream out) throws java.io.UnsupportedEncodingException
protected java.io.Writer makeWriter(java.io.OutputStream out, java.lang.String encoding) throws java.io.UnsupportedEncodingException
public void output(Document doc, java.io.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.
doc
- Document
to format.out
- OutputStream
to write to.IOException
- - if there's any problem writing.public void output(Document doc, java.io.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.
doc
- Document
to format.out
- Writer
to write to.IOException
- - if there's any problem writing.public void output(Element element, java.io.Writer out) throws java.io.IOException
Print out an
, including
its Element
s, and its value, and all
contained (child) elements etc.
Attribute
element
- Element
to output.out
- Writer
to write to.public void output(Element element, java.io.OutputStream out) throws java.io.IOException
Print out an
, including
its Element
s, and its value, and all
contained (child) elements etc.
Attribute
element
- Element
to output.out
- Writer
to write to.public void output(CDATA cdata, java.io.Writer out) throws java.io.IOException
Print out a CDATA
cdata
- CDATA
to output.out
- Writer
to write to.public void output(CDATA cdata, java.io.OutputStream out) throws java.io.IOException
Print out a CDATA
cdata
- CDATA
to output.out
- OutputStream
to write to.public void output(Comment comment, java.io.Writer out) throws java.io.IOException
Print out a Comment
comment
- Comment
to output.out
- Writer
to write to.public void output(Comment comment, java.io.OutputStream out) throws java.io.IOException
Print out a Comment
comment
- Comment
to output.out
- OutputStream
to write to.public void output(java.lang.String string, java.io.Writer out) throws java.io.IOException
Print out a
. Perfoms
the necessary entity escaping and whitespace stripping. String
string
- String
to output.out
- Writer
to write to.public void output(java.lang.String string, java.io.OutputStream out) throws java.io.IOException
Print out a
. Perfoms
the necessary entity escaping and whitespace stripping. String
cdata
- CDATA
to output.out
- OutputStream
to write to.public void output(Entity entity, java.io.Writer out) throws java.io.IOException
Print out an
.
Entity
entity
- Entity
to output.out
- Writer
to write to.public void output(Entity entity, java.io.OutputStream out) throws java.io.IOException
Print out an
.
Entity
cdata
- CDATA
to output.out
- OutputStream
to write to.public void output(ProcessingInstruction processingInstruction, java.io.Writer out) throws java.io.IOException
Print out a ProcessingInstruction
element
- ProcessingInstruction
to output.out
- Writer
to write to.public void output(ProcessingInstruction processingInstruction, java.io.OutputStream out) throws java.io.IOException
Print out a ProcessingInstruction
processingInstruction
- ProcessingInstruction
to output.out
- OutputStream
to write to.public java.lang.String outputString(Document doc) throws java.io.IOException
doc
- Document
to format.public java.lang.String outputString(Element element) throws java.io.IOException
doc
- Element
to format.public void printDeclaration(Document doc, java.io.Writer out, java.lang.String encoding) throws java.io.IOException
This will write the declaration to the given Writer. Assumes XML version 1.0 since we don't directly know.
docType
- DocType
whose declaration to write.out
- Writer
to write to.public void printDocType(DocType docType, java.io.Writer out) throws java.io.IOException
This will write the DOCTYPE declaration if one exists.
doc
- Document
whose declaration to write.out
- Writer
to write to.protected void printComment(Comment comment, java.io.Writer out, int indentLevel) throws java.io.IOException
This will write the comment to the specified writer.
comment
- Comment
to write.out
- Writer
to write to.indentLevel
- Current depth in hierarchy.protected void printProcessingInstruction(ProcessingInstruction pi, java.io.Writer out, int indentLevel) throws java.io.IOException
This will write the processing instruction to the specified writer.
comment
- ProcessingInstruction
to write.out
- Writer
to write to.indentLevel
- Current depth in hierarchy.protected void printCDATASection(CDATA cdata, java.io.Writer out, int indentLevel) throws java.io.IOException
This will handle printing out an
,
and its value.
CDATA
cdata
- CDATA
to output.out
- Writer
to write to.indent
- int
level of indention.protected void printElement(Element element, java.io.Writer out, int indentLevel, org.jdom.output.NamespaceStack namespaces) throws java.io.IOException
This will handle printing out an
,
its Element
s, and its value.
Attribute
element
- Element
to output.out
- Writer
to write to.indent
- int
level of indention.namespaces
- List
stack of Namespaces in scope.public void printElementContent(Element element, java.io.Writer out) throws java.io.IOException
This will handle printing out an
's content only, not including its tag,
attributes, and namespace info. Element
element
- Element
to output.out
- Writer
to write to.indent
- int
level of indention.protected void printElementContent(Element element, java.io.Writer out, int indentLevel, org.jdom.output.NamespaceStack namespaces, java.util.List mixedContent) throws java.io.IOException
This will handle printing out an
's content only, not including its tag,
attributes, and namespace info. Element
element
- Element
to output.out
- Writer
to write to.indent
- int
level of indention.protected void printString(java.lang.String s, java.io.Writer out) throws java.io.IOException
protected void printEntity(Entity entity, java.io.Writer out) throws java.io.IOException
This will handle printing out an
.
Only the entity reference such as Entity
&entity;
will be printed. However, subclasses are free to override
this method to print the contents of the entity instead.
entity
- Entity
to output.out
- Writer
to write to.protected void printNamespace(Namespace ns, java.io.Writer out) throws java.io.IOException
This will handle printing out any needed
declarations.
Namespace
ns
- Namespace
to print definition ofout
- Writer
to write to.protected void printAttributes(java.util.List attributes, Element parent, java.io.Writer out, org.jdom.output.NamespaceStack namespaces) throws java.io.IOException
This will handle printing out an
list.
Attribute
attributes
- List
of Attribute objctsout
- Writer
to write topublic int parseArgs(java.lang.String[] args, int i)
-omitEncoding
-indentSize 3 ...
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |