org.dbunit.util.xml
Class XmlWriter

java.lang.Object
  extended by org.dbunit.util.xml.XmlWriter

public class XmlWriter
extends Object

Makes writing XML much much easier. Improved from article

Since:
1.0
Version:
$Revision: 859 $ $Date: 2008-11-02 12:50:23 +0100 (Sun, 02 Nov 2008) $
Author:
Henri Yandell, Peter Cassetta, Last changed by: $Author: gommma $

Field Summary
static String CDATA_END
          CDATA end tag: "]]>"
static String CDATA_START
          CDATA start tag: "<![CDATA["
static String DEFAULT_ENCODING
          Default encoding value which is "UTF-8"
 
Constructor Summary
XmlWriter(OutputStream outputStream, String encoding)
          Create an XmlWriter on top of an existing OutputStream.
XmlWriter(Writer writer)
          Create an XmlWriter on top of an existing java.io.Writer.
XmlWriter(Writer writer, String encoding)
          Create an XmlWriter on top of an existing java.io.Writer.
 
Method Summary
 void close()
          Close this writer.
 void enablePrettyPrint(boolean enable)
          Turn pretty printing on or off.
 XmlWriter endElement()
          End the current element.
static void main(String[] args)
           
 void setIndent(String indent)
          Specify the string to prepend to a line for each level of indent.
 void setNewline(String newline)
          Specify the string used to terminate each line when pretty printing.
 void setWriter(Writer writer, String encoding)
          Resets the handler to write a new text document.
static void test1()
           
static void test2()
           
 XmlWriter writeAttribute(String attr, String value)
          Write an attribute out for the current element.
 XmlWriter writeAttribute(String attr, String value, boolean literally)
          Write an attribute out for the current element.
 XmlWriter writeCData(String cdata)
          Write out a chunk of CDATA.
 XmlWriter writeComment(String comment)
          Write out a chunk of comment.
 XmlWriter writeDeclaration()
           
 XmlWriter writeDoctype(String systemId, String publicId)
           
 XmlWriter writeElement(String name)
          Begin to write out an element.
 XmlWriter writeElementWithText(String name, String text)
          A helper method.
 XmlWriter writeEmptyElement(String name)
          A helper method.
 XmlWriter writeText(String text)
          Output body text.
 XmlWriter writeText(String text, boolean literally)
          Output body text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CDATA_START

public static final String CDATA_START
CDATA start tag: "<![CDATA["

See Also:
Constant Field Values

CDATA_END

public static final String CDATA_END
CDATA end tag: "]]>"

See Also:
Constant Field Values

DEFAULT_ENCODING

public static final String DEFAULT_ENCODING
Default encoding value which is "UTF-8"

See Also:
Constant Field Values
Constructor Detail

XmlWriter

public XmlWriter(Writer writer)
Create an XmlWriter on top of an existing java.io.Writer.


XmlWriter

public XmlWriter(Writer writer,
                 String encoding)
Create an XmlWriter on top of an existing java.io.Writer.


XmlWriter

public XmlWriter(OutputStream outputStream,
                 String encoding)
          throws UnsupportedEncodingException
Create an XmlWriter on top of an existing OutputStream.

Parameters:
outputStream -
encoding - The encoding to be used for writing to the given output stream. Can be null. If it is null the DEFAULT_ENCODING is used.
Throws:
UnsupportedEncodingException
Since:
2.4
Method Detail

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

public void setIndent(String indent)
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

public void setNewline(String newline)
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

public XmlWriter writeElementWithText(String name,
                                      String text)
                               throws IOException
A helper method. It writes out an element which contains only text.

Parameters:
name - String name of tag
text - String of text to go inside the tag
Throws:
IOException

writeEmptyElement

public XmlWriter writeEmptyElement(String name)
                            throws IOException
A helper method. It writes out empty entities.

Parameters:
name - String name of tag
Throws:
IOException

writeElement

public XmlWriter writeElement(String name)
                       throws IOException
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
Throws:
IOException

writeAttribute

public XmlWriter writeAttribute(String attr,
                                String value)
                         throws IOException
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.
Throws:
IOException
See Also:
writeAttribute(String, String, boolean)

writeAttribute

public XmlWriter writeAttribute(String attr,
                                String value,
                                boolean literally)
                         throws IOException
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.
Throws:
IOException

endElement

public XmlWriter endElement()
                     throws IOException
End the current element. This will throw an exception if it is called when there is not a currently open element.

Throws:
IOException

close

public void close()
           throws IOException
Close this writer. It does not close the underlying writer, but does throw an exception if there are as yet unclosed tags.

Throws:
IOException

writeText

public XmlWriter writeText(String text)
                    throws IOException
Output body text. Any XML characters are escaped.

Parameters:
text - The text to be written
Returns:
This writer
Throws:
IOException
See Also:
writeText(String, boolean)

writeText

public XmlWriter writeText(String text,
                           boolean literally)
                    throws IOException
Output body text. Any XML characters are escaped.

Parameters:
text - The text to be written
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

writeCData

public XmlWriter writeCData(String cdata)
                     throws IOException
Write out a chunk of CDATA. This helper method surrounds the passed in data with the CDATA tag.

Parameters:
cdata - of CDATA text.
Throws:
IOException

writeComment

public XmlWriter writeComment(String comment)
                       throws IOException
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.
Throws:
IOException

main

public static void main(String[] args)
                 throws IOException
Throws:
IOException

test1

public static void test1()
                  throws IOException
Throws:
IOException

test2

public static void test2()
                  throws IOException
Throws:
IOException

setWriter

public final void setWriter(Writer writer,
                            String encoding)
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 stream out is not null)

writeDeclaration

public XmlWriter writeDeclaration()
                           throws IOException
Throws:
IOException

writeDoctype

public XmlWriter writeDoctype(String systemId,
                              String publicId)
                       throws IOException
Throws:
IOException


Copyright © 2002-2012. All Rights Reserved.