Class XMLOutputter
- java.lang.Object
-
- org.jdom.output.XMLOutputter
-
- All Implemented Interfaces:
Cloneable
public class XMLOutputter extends Object implements Cloneable
Outputs a JDOM document as a stream of bytes. The outputter can manage many styles of document formatting, from untouched to pretty printed. The default is to output the document content exactly as created, but this can be changed by setting a new Format object. For pretty-print output, use. For whitespace-normalized output, useFormat.getPrettyFormat().Format.getCompactFormat()There are
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 theoutput(...)method. The default encoding is UTF-8.Format.setEncoding(java.lang.String)The methods
are for convenience only; for top performance you should call one of theoutputString(...)methods and pass in your own Writer or OutputStream if possible.output(...)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
. To omit printing of the encoding in the declaration useFormat.setOmitDeclaration(boolean). Unfortunatly there is currently no way to know the original encoding of the document.Format.setOmitEncoding(boolean)Empty elements are by default printed as <empty/>, but this can be configured with
to cause them to be expanded to <empty></empty>.Format.setExpandEmptyElements(boolean)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classXMLOutputter.NamespaceStackOur own null subclass of NamespaceStack.
-
Field Summary
Fields Modifier and Type Field Description protected FormatcurrentFormatprotected static FormatpreserveFormat
-
Constructor Summary
Constructors Constructor Description XMLOutputter()XMLOutputter(Format format)This will create anXMLOutputterwith the specified format characteristics.XMLOutputter(XMLOutputter that)This will create anXMLOutputterwith all the options as set in the givenXMLOutputter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectclone()Returns a copy of this XMLOutputter.StringescapeAttributeEntities(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.StringescapeElementEntities(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.FormatgetFormat()Returns the current format in use by the outputter.voidoutput(List list, OutputStream out)This will handle printing out a list of nodes.voidoutput(List list, Writer out)This will handle printing out a list of nodes.voidoutput(CDATA cdata, OutputStream out)Print out anode.CDATAvoidoutput(CDATA cdata, Writer out)Print out anode.CDATAvoidoutput(Comment comment, OutputStream out)Print out a.Commentvoidoutput(Comment comment, Writer out)Print out a.Commentvoidoutput(DocType doctype, OutputStream out)Print out the.DocTypevoidoutput(DocType doctype, Writer out)Print out the.DocTypevoidoutput(Document doc, OutputStream out)This will print theDocumentto the given output stream.voidoutput(Document doc, Writer out)This will print theDocumentto the given Writer.voidoutput(Element element, OutputStream out)voidoutput(Element element, Writer out)voidoutput(EntityRef entity, OutputStream out)Print out a.EntityRefvoidoutput(EntityRef entity, Writer out)Print out a.EntityRefvoidoutput(ProcessingInstruction pi, OutputStream out)Print out a.ProcessingInstructionvoidoutput(ProcessingInstruction pi, Writer out)Print out a.ProcessingInstructionvoidoutput(Text text, OutputStream out)Print out anode.Textvoidoutput(Text text, Writer out)Print out anode.TextvoidoutputElementContent(Element element, OutputStream out)This will handle printing out an's content only, not including its tag, and attributes.ElementvoidoutputElementContent(Element element, Writer out)This will handle printing out an's content only, not including its tag, and attributes.ElementStringoutputString(List list)Return a string representing a list of nodes.StringoutputString(CDATA cdata)Return a string representing a CDATA node.StringoutputString(Comment comment)Return a string representing a comment.StringoutputString(DocType doctype)Return a string representing a DocType.StringoutputString(Document doc)Return a string representing a document.StringoutputString(Element element)Return a string representing an element.StringoutputString(EntityRef entity)Return a string representing an entity.StringoutputString(ProcessingInstruction pi)Return a string representing a PI.StringoutputString(Text text)Return a string representing a Text node.protected voidprintAttributes(Writer out, List attributes, Element parent, XMLOutputter.NamespaceStack namespaces)This will handle printing of alist.Attributeprotected voidprintCDATA(Writer out, CDATA cdata)This will handle printing oftext.CDATAprotected voidprintComment(Writer out, Comment comment)This will handle printing of comments.protected voidprintDeclaration(Writer out, Document doc, String encoding)This will handle printing of the declaration.protected voidprintDocType(Writer out, DocType docType)This handle printing the DOCTYPE declaration if one exists.protected voidprintElement(Writer out, Element element, int level, XMLOutputter.NamespaceStack namespaces)protected voidprintEntityRef(Writer out, EntityRef entity)This will handle printing a.EntityRefprotected voidprintProcessingInstruction(Writer out, ProcessingInstruction pi)This will handle printing of processing instructions.protected voidprintText(Writer out, Text text)This will handle printing ofstrings.TextvoidsetFormat(Format newFormat)Sets the new format logic for the outputter.StringtoString()Return a string listing of the settings for this XMLOutputter instance.
-
-
-
Constructor Detail
-
XMLOutputter
public XMLOutputter()
-
XMLOutputter
public XMLOutputter(Format format)
This will create anXMLOutputterwith the specified format characteristics. Note the format object is cloned internally before use.
-
XMLOutputter
public XMLOutputter(XMLOutputter that)
This will create anXMLOutputterwith all the options as set in the givenXMLOutputter. Note thatXMLOutputter two = (XMLOutputter)one.clone();would work equally well.- Parameters:
that- the XMLOutputter to clone
-
-
Method Detail
-
setFormat
public void setFormat(Format newFormat)
Sets the new format logic for the outputter. Note the Format object is cloned internally before use.- Parameters:
newFormat- the format to use for output
-
getFormat
public Format getFormat()
Returns the current format in use by the outputter. Note the Format object returned is a clone of the one used internally.
-
output
public void output(Document doc, OutputStream out) throws IOException
This will print theDocumentto the given output stream. The characters are printed using the encoding specified in the constructor, or a default of UTF-8.- Parameters:
doc-Documentto format.out-OutputStreamto use.- Throws:
IOException- - if there's any problem writing.
-
output
public void output(DocType doctype, OutputStream out) throws IOException
Print out the.DocType- Parameters:
doctype-DocTypeto output.out-OutputStreamto use.- Throws:
IOException
-
output
public void output(Element element, OutputStream out) throws IOException
- Parameters:
element-Elementto output.out-Writerto use.- Throws:
IOException
-
outputElementContent
public void outputElementContent(Element element, OutputStream out) throws IOException
This will handle printing out an'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- Parameters:
element-Elementto output.out-OutputStreamto use.- Throws:
IOException
-
output
public void output(List list, OutputStream out) throws IOException
This will handle printing out a list of nodes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".- Parameters:
list-Listof nodes.out-OutputStreamto use.- Throws:
IOException
-
output
public void output(CDATA cdata, OutputStream out) throws IOException
Print out anode.CDATA- Parameters:
cdata-CDATAto output.out-OutputStreamto use.- Throws:
IOException
-
output
public void output(Text text, OutputStream out) throws IOException
Print out anode. Perfoms the necessary entity escaping and whitespace stripping.Text- Parameters:
text-Textto output.out-OutputStreamto use.- Throws:
IOException
-
output
public void output(Comment comment, OutputStream out) throws IOException
Print out a.Comment- Parameters:
comment-Commentto output.out-OutputStreamto use.- Throws:
IOException
-
output
public void output(ProcessingInstruction pi, OutputStream out) throws IOException
Print out a.ProcessingInstruction- Parameters:
pi-ProcessingInstructionto output.out-OutputStreamto use.- Throws:
IOException
-
output
public void output(EntityRef entity, OutputStream out) throws IOException
Print out a.EntityRef- Parameters:
entity-EntityRefto output.out-OutputStreamto use.- Throws:
IOException
-
output
public void output(Document doc, Writer out) throws IOException
This will print theDocumentto 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-Documentto format.out-Writerto use.- Throws:
IOException- - if there's any problem writing.
-
output
public void output(DocType doctype, Writer out) throws IOException
Print out the.DocType- Parameters:
doctype-DocTypeto output.out-Writerto use.- Throws:
IOException
-
output
public void output(Element element, Writer out) throws IOException
- Parameters:
element-Elementto output.out-Writerto use.- Throws:
IOException
-
outputElementContent
public void outputElementContent(Element element, Writer out) throws IOException
This will handle printing out an'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- Parameters:
element-Elementto output.out-Writerto use.- Throws:
IOException
-
output
public void output(List list, Writer out) throws IOException
This will handle printing out a list of nodes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".- Parameters:
list-Listof nodes.out-Writerto use.- Throws:
IOException
-
output
public void output(CDATA cdata, Writer out) throws IOException
Print out anode.CDATA- Parameters:
cdata-CDATAto output.out-Writerto use.- Throws:
IOException
-
output
public void output(Text text, Writer out) throws IOException
Print out anode. Perfoms the necessary entity escaping and whitespace stripping.Text- Parameters:
text-Textto output.out-Writerto use.- Throws:
IOException
-
output
public void output(Comment comment, Writer out) throws IOException
Print out a.Comment- Parameters:
comment-Commentto output.out-Writerto use.- Throws:
IOException
-
output
public void output(ProcessingInstruction pi, Writer out) throws IOException
Print out a.ProcessingInstruction- Parameters:
pi-ProcessingInstructionto output.out-Writerto use.- Throws:
IOException
-
output
public void output(EntityRef entity, Writer out) throws IOException
Print out a.EntityRef- Parameters:
entity-EntityRefto output.out-Writerto use.- Throws:
IOException
-
outputString
public String outputString(Document doc)
Return a string representing a document. Uses an internal StringWriter. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
doc-Documentto format.
-
outputString
public String outputString(DocType doctype)
Return a string representing a DocType. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
doctype-DocTypeto format.
-
outputString
public String outputString(Element element)
Return a string representing an element. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
element-Elementto format.
-
outputString
public String outputString(List list)
Return a string representing a list of nodes. The list is assumed to contain legal JDOM nodes.- Parameters:
list-Listto format.
-
outputString
public String outputString(CDATA cdata)
Return a string representing a CDATA node. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
cdata-CDATAto format.
-
outputString
public String outputString(Text text)
Return a string representing a Text node. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
text-Textto format.
-
outputString
public String outputString(Comment comment)
Return a string representing a comment. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
comment-Commentto format.
-
outputString
public String outputString(ProcessingInstruction pi)
Return a string representing a PI. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
pi-ProcessingInstructionto format.
-
outputString
public String outputString(EntityRef entity)
Return a string representing an entity. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
entity-EntityRefto format.
-
printDeclaration
protected void printDeclaration(Writer out, Document doc, String encoding) throws IOException
This will handle printing of the declaration. Assumes XML version 1.0 since we don't directly know.- Parameters:
doc-Documentwhose declaration to write.out-Writerto use.encoding- The encoding to add to the declaration- Throws:
IOException
-
printDocType
protected void printDocType(Writer out, DocType docType) throws IOException
This handle printing the DOCTYPE declaration if one exists.- Parameters:
docType-Documentwhose declaration to write.out-Writerto use.- Throws:
IOException
-
printComment
protected void printComment(Writer out, Comment comment) throws IOException
This will handle printing of comments.- Parameters:
comment-Commentto write.out-Writerto use.- Throws:
IOException
-
printProcessingInstruction
protected void printProcessingInstruction(Writer out, ProcessingInstruction pi) throws IOException
This will handle printing of processing instructions.- Parameters:
pi-ProcessingInstructionto write.out-Writerto use.- Throws:
IOException
-
printEntityRef
protected void printEntityRef(Writer out, EntityRef entity) throws IOException
This will handle printing a. Only the entity reference such asEntityRef&entity;will be printed. However, subclasses are free to override this method to print the contents of the entity instead.- Parameters:
entity-EntityRefto output.out-Writerto use.- Throws:
IOException
-
printCDATA
protected void printCDATA(Writer out, CDATA cdata) throws IOException
This will handle printing oftext.CDATA- Parameters:
cdata-CDATAto output.out-Writerto use.- Throws:
IOException
-
printText
protected void printText(Writer out, Text text) throws IOException
This will handle printing ofstrings.Text- Parameters:
text-Textto write.out-Writerto use.- Throws:
IOException
-
printElement
protected void printElement(Writer out, Element element, int level, XMLOutputter.NamespaceStack namespaces) throws IOException
- Parameters:
element-Elementto output.out-Writerto use.level-intlevel of indention.namespaces-Liststack of Namespaces in scope.- Throws:
IOException
-
printAttributes
protected void printAttributes(Writer out, List attributes, Element parent, XMLOutputter.NamespaceStack namespaces) throws IOException
This will handle printing of alist.Attribute- Parameters:
attributes-Listof Attribute objctsout-Writerto use- Throws:
IOException
-
escapeAttributeEntities
public 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. It does not convert the single quote (') because it's not necessary as the outputter writes attributes surrounded by double-quotes.- Parameters:
str-Stringinput to escape.- Returns:
Stringwith escaped content.- Throws:
IllegalArgumentException- if an entity can not be escaped
-
escapeElementEntities
public 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.- Parameters:
str-Stringinput to escape.- Returns:
Stringwith escaped content.- Throws:
IllegalArgumentException- if an entity can not be escaped
-
-