Package org.dbunit.util.xml
Class XmlWriter
java.lang.Object
org.dbunit.util.xml.XmlWriter
Makes writing XML much much easier. Improved from
article
- Since:
- 1.0
- Version:
- $Revision$ $Date$
- Author:
- Henri Yandell, Peter Cassetta, Last changed by: $Author$
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionXmlWriter(OutputStream outputStream, Charset charset) Create an XmlWriter on top of an existingOutputStream.Create an XmlWriter on top of an existing java.io.Writer.Create an XmlWriter on top of an existing java.io.Writer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close this writer.protected StringconvertCharacterToEntity(char currentChar, boolean literally) Returns the XML entity for the given character, if any.voidenablePrettyPrint(boolean enable) Turn pretty printing on or off.End the current element.voidflush()Flushes the underlying writer, withoutclose()'s unclosed-tags check.static voidvoidSpecify the string to prepend to a line for each level of indent.voidsetNewline(String newline) Specify the string used to terminate each line when pretty printing.final voidResets the handler to write a new text document.final voidSets the writer and character set to write to.static voidtest1()Writes an example XML document using the fluentwriteElement(String)style.static voidtest2()Writes the same example XML document astest1(), using the step-by-step style.writeAttribute(String attr, String value) Write an attribute out for the current element.writeAttribute(String attr, String value, boolean literally) Write an attribute out for the current element.writeCData(String cdata) Write out a chunk of CDATA.writeComment(String comment) Write out a chunk of comment.Writes the XML declaration, if an encoding is set.writeDoctype(String systemId, String publicId) Writes aDOCTYPEdeclaration for the dataset, if a system or public id is given.writeElement(String name) Begin to write out an element.writeElementWithText(String name, String text) A helper method.writeEmptyElement(String name) A helper method.Output body text.Output body text.
-
Field Details
-
CDATA_START
CDATA start tag: "<![CDATA["- See Also:
-
CDATA_END
CDATA end tag: "]]>"- See Also:
-
DEFAULT_ENCODING
Default encoding value which is "UTF-8"- See Also:
-
DEFAULT_CHARSET
Default charset, "UTF-8".
-
-
Constructor Details
-
XmlWriter
Create an XmlWriter on top of an existing java.io.Writer.- Parameters:
writer- the writer to write to.
-
XmlWriter
Create an XmlWriter on top of an existing java.io.Writer.- Parameters:
writer- the writer to write to.charset- the charset to declare in the XML prolog, may benull.
-
XmlWriter
Create an XmlWriter on top of an existingOutputStream.- Parameters:
outputStream- the stream to write to.charset- The charset to be used for writing to the given output stream. Can benull. If it isnulltheDEFAULT_ENCODINGis used.- Since:
- 2.4
-
-
Method Details
-
enablePrettyPrint
public void enablePrettyPrint(boolean enable) Turn pretty printing on or off. Pretty printing is enabled by default, but it can be turned off to generate more compact XML.- Parameters:
enable- true to enable, false to disable pretty printing.
-
setIndent
Specify the string to prepend to a line for each level of indent. It is 2 spaces (" ") by default. Some may prefer a single tab ("\t") or a different number of spaces. Specifying an empty string will turn off indentation when pretty printing.- Parameters:
indent- representing one level of indentation while pretty printing.
-
setNewline
Specify the string used to terminate each line when pretty printing. It is a single newline ("\n") by default. Users who need to read generated XML documents in Windows editors like Notepad may wish to set this to a carriage return/newline sequence ("\r\n"). Specifying an empty string will turn off generation of line breaks when pretty printing.- Parameters:
newline- representing the newline sequence when pretty printing.
-
writeElementWithText
A helper method. It writes out an element which contains only text.- Parameters:
name- String name of tagtext- String of text to go inside the tag- Returns:
- this writer.
- Throws:
IOException- if writing fails.
-
writeEmptyElement
A helper method. It writes out empty entities.- Parameters:
name- String name of tag- Returns:
- this writer.
- Throws:
IOException- if writing fails.
-
writeElement
Begin to write out an element. Unlike the helper tags, this tag will need to be ended with the endElement method.- Parameters:
name- String name of tag- Returns:
- this writer.
- Throws:
IOException- if writing fails.
-
writeAttribute
Write an attribute out for the current element. Any XML characters in the value are escaped. Currently it does not actually throw the exception, but the API is set that way for future changes.- Parameters:
attr- name of attribute.value- value of attribute.- Returns:
- this writer.
- Throws:
IOException- if writing fails.- See Also:
-
writeAttribute
Write an attribute out for the current element. Any XML characters in the value are escaped. Currently it does not actually throw the exception, but the API is set that way for future changes.- Parameters:
attr- name of attribute.value- value of attribute.literally- If the writer should be literally on the given value which means that meta characters will also be preserved by escaping them. Mainly preserves newlines and tabs.- Returns:
- this writer.
- Throws:
IOException- if writing fails.
-
endElement
End the current element. This will throw an exception if it is called when there is not a currently open element.- Returns:
- this writer.
- Throws:
IOException- if there is no currently open element, or writing fails.
-
close
Close this writer. It does not close the underlying writer, but does throw an exception if there are as yet unclosed tags.- Throws:
IOException- if there are unclosed tags, or flushing fails.
-
flush
Flushes the underlying writer, withoutclose()'s unclosed-tags check. Intended for callers recovering from a write error mid-document, so already-written content reaches the underlying stream even though the document itself is incomplete.- Throws:
IOException- on a failure to flush the underlying writer.
-
writeText
Output body text. Any XML characters are escaped.- Parameters:
text- The text to be written- Returns:
- This writer
- Throws:
IOException- if writing fails.- See Also:
-
writeText
Output body text. Any XML characters are escaped.- Parameters:
text- The text to be writtenliterally- If the writer should be literally on the given value which means that meta characters will also be preserved by escaping them. Mainly preserves newlines and tabs.- Returns:
- This writer
- Throws:
IOException- if writing fails.
-
writeCData
Write out a chunk of CDATA. This helper method surrounds the passed in data with the CDATA tag.- Parameters:
cdata- of CDATA text.- Returns:
- this writer.
- Throws:
IOException- if writing fails.
-
writeComment
Write out a chunk of comment. This helper method surrounds the passed in data with the XML comment tag.- Parameters:
comment- of text to comment.- Returns:
- this writer.
- Throws:
IOException- if writing fails.
-
main
- Parameters:
args- ignored.- Throws:
IOException- if writing the example XML fails.
-
test1
Writes an example XML document using the fluentwriteElement(String)style.- Throws:
IOException- if writing the example XML fails.
-
test2
Writes the same example XML document astest1(), using the step-by-step style.- Throws:
IOException- if writing the example XML fails.
-
convertCharacterToEntity
Returns the XML entity for the given character, if any.- Parameters:
currentChar- the character to convert.literally- whether the character was written via a "literal" write method, which affects which characters are converted.- Returns:
- the XML entity for the given character, or
nullif it needs no entity.
-
setWriter
Resets the handler to write a new text document.- Parameters:
writer- XML text is written to this writer.encoding- if non-null, and an XML declaration is written, this is the name that will be used for the character encoding.- Throws:
IllegalStateException- if the current document hasn't yet ended (i.e. the output streamoutis not null)
-
setWriter
Sets the writer and character set to write to.- Parameters:
writer- the writer to write to.charset- the character set to encode the declaration with.
-
writeDeclaration
Writes the XML declaration, if an encoding is set.- Returns:
- this writer, for chaining.
- Throws:
IOException- if writing to the underlying stream fails.
-
writeDoctype
Writes aDOCTYPEdeclaration for the dataset, if a system or public id is given.- Parameters:
systemId- the DTD's system id, ornull.publicId- the DTD's public id, ornull.- Returns:
- this writer, for chaining.
- Throws:
IOException- if writing to the underlying stream fails.
-