Use Commons Betwixt BeanWriter
to transform a bean to an XML document. The following code
turns an instance of the Play object
from Recipe 6.2 into an XML
document:
import org.apache.commons.betwixt.io.BeanWriter; Play play = new Play( ); // populatePlay populates all properties and nested Character objects populatePlay( play ); // Write XML document BeanWriter beanWriter = new BeanWriter( ); beanWriter.enablePrettyPrint( ); beanWriter.write( play ); System.out.println( beanWriter.toString( ) );
A BeanWriter instance is
created, indentation is enabled with a call to enablePrettyPrint( ), and the Play object is written to an XML document with
beanWriter.write( ). The previous
example prints an XML document with structure similar to the XML
document parsed in Recipe
6.2 with the exception of the genre, year, and language elements. The following XML document
is produced by the call to beanWriter.write() :
<Play>
<author>William Shakespeare</author>
<characters>
<character>
<description>King of Denmark</description>
<name>Claudius</name>
<protagonist>false</protagonist>
</character>
<character>
<description>
Son to the late, and nephew of the present
king
</description>
<name>Hamlet</name>
<protagonist>true</protagonist>
</character>
<character>
<description>friend to Hamlet</description>
<name>Horatio</name>
<protagonist>false</protagonist>
</character>
</characters>
<genre>tragedy</genre>
<language>english</language>
<name>Hamlet</name>
<summary>
Prince of Denmark (Hamlet) freaks out, talks to
father's ghost, and finally dies in a duel.
</summary>
<year>1603</year>
</Play>Using the BeanWriter is an easy
way to create XML documents from beans, and if you don't have a
preference for the layout of the resulting XML document, it is the
easiest way to serialize an object to XML. The BeanWriter offers some control over the
appearance of the XML it generates. setEndOfLine( ) takes a String that is used as a line termination
sequence. setWriteEmptyElements( )
controls the way in which an empty element is written by Betwixt. If
setWriteEmptyElements( ) is passed
true, an element will be written to
the XML document even if there are no child nodes or attributes.
setIndent( ) takes a String that is used as an indentation string
when pretty printing—indented output—is enabled using the enablePrettyPrint( ) method on BeanWriter.
When Betwixt encounters the List of Character objects, it creates an element
characters, which holds individual
character elements created by using
introspection on the Character class.
This behavior is configurable, and you can instruct Betwixt to omit the characters elements in favor of multiple
character child elements. Such a
customization is demonstrated in the Discussion of Recipe 6.8.
BeanWriter can also be used to
write an XML document to an OutputStream or a Writer by passing a Writer or an OutputStream to the BeanWriter constructor. The following code
uses a BeanWriter to write an XML
document to test.dat:
import org.apache.commons.betwixt.io.BeanWriter;
Play play = new Play( );
populatePlay( play );
// Open a File Writer
Writer outputWriter = new FileWriter("test.dat");
// Pass FileWriter to BeanWriter
BeanWriter beanWriter = new BeanWriter( outputWriter );
beanWriter.setEndOfLine( "\r\n" );
beanWriter.setIndent( "\t" );
beanWriter.enablePrettyPrint( );
beanWriter.write( play );
// Close FileWriter
outputWriter.close( );Since the previous example contains a call to setEndOfLine(), enablePrettyPrint( ), and setIndent( ), test.dat will have DOS-style line termination
and Betwixt will use the tab character for indentation.
By default, BeanWriter writes
every bean property of Play as an element. Recipe 6.8 shows you how to
customize the XML generated by Betwixt.
