org.dbunit.util.xml
Class XmlWriter

java.lang.Object
  extended byorg.dbunit.util.xml.XmlWriter

public class XmlWriter
extends java.lang.Object

Makes writing XML much much easier. Improved from article

Version:
1.0
Author:
Henri Yandell, Peter Cassetta

Constructor Summary
XmlWriter(java.io.Writer writer)
          Create an XmlWriter on top of an existing java.io.Writer.
XmlWriter(java.io.Writer writer, java.lang.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(java.lang.String[] args)
           
 void setIndent(java.lang.String indent)
          Specify the string to prepend to a line for each level of indent.
 void setNewline(java.lang.String newline)
          Specify the string used to terminate each line when pretty printing.
 void setWriter(java.io.Writer writer, java.lang.String encoding)
          Resets the handler to write a new text document.
static void test1()
           
static void test2()
           
 XmlWriter writeAttribute(java.lang.String attr, java.lang.String value)
          Write an attribute out for the current element.
 XmlWriter writeCData(java.lang.String cdata)
          Write out a chunk of CDATA.
 XmlWriter writeComment(java.lang.String comment)
          Write out a chunk of comment.
 XmlWriter writeDeclaration()
           
 XmlWriter writeDoctype(java.lang.String systemId, java.lang.String publicId)
           
 XmlWriter writeElement(java.lang.String name)
          Begin to write out an element.
 XmlWriter writeElementWithText(java.lang.String name, java.lang.String text)
          A helper method.
 XmlWriter writeEmptyElement(java.lang.String name)
          A helper method.
 XmlWriter writeText(java.lang.String text)
          Output body text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlWriter

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


XmlWriter

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

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(java.lang.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(java.lang.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(java.lang.String name,
                                      java.lang.String text)
                               throws java.io.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:
java.io.IOException

writeEmptyElement

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

Parameters:
name - String name of tag
Throws:
java.io.IOException

writeElement

public XmlWriter writeElement(java.lang.String name)
                       throws java.io.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:
java.io.IOException

writeAttribute

public XmlWriter writeAttribute(java.lang.String attr,
                                java.lang.String value)
                         throws java.io.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:
java.io.IOException

endElement

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

Throws:
java.io.IOException

close

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

Throws:
java.io.IOException

writeText

public XmlWriter writeText(java.lang.String text)
                    throws java.io.IOException
Output body text. Any xml characters are escaped.

Throws:
java.io.IOException

writeCData

public XmlWriter writeCData(java.lang.String cdata)
                     throws java.io.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:
java.io.IOException

writeComment

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

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Throws:
java.io.IOException

test1

public static void test1()
                  throws java.io.IOException
Throws:
java.io.IOException

test2

public static void test2()
                  throws java.io.IOException
Throws:
java.io.IOException

setWriter

public final void setWriter(java.io.Writer writer,
                            java.lang.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:
java.lang.IllegalStateException - if the current document hasn't yet ended (with #endDocument)

writeDeclaration

public XmlWriter writeDeclaration()
                           throws java.io.IOException
Throws:
java.io.IOException

writeDoctype

public XmlWriter writeDoctype(java.lang.String systemId,
                              java.lang.String publicId)
                       throws java.io.IOException
Throws:
java.io.IOException


Copyright © 2002-2005 DbUnit.org. All Rights Reserved.