November 2004
Intermediate to advanced
400 pages
10h 31m
English
You need to create an XML document from a bean.
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> ...
Read now
Unlock full access