Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

6.7. Turning Beans into XML Documents

6.7.1. Problem

You need to create an XML document from a bean.

6.7.2. Solution

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>

6.7.3. Discussion

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.

6.7.4. See Also

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.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.