Getting StartedProbably the best way to get started is to look at some examples. Here is some simple example code that reads and writes a bean (together with some comments). It's a good place to start. Mapping beans to XMLThere are various ways of mapping beans to an XML structure. For example consider a simple bean public class CustomerBean { public String getName(); public Order[] getOrders(); public String[] getEmailAddresses(); } This could be mapped to XML in various ways. A couple of examples of these different ways follow. Example 1This example uses attributes for primitive types. <CustomerBean name='James'> <order id='1'>...</order> <order id='2'>...</order> <emailAddress>jstrachan@apache.org</emailAddress> </CustomerBean> Example 2This example uses elements for all properties and wraps collections in an extra element (which can be quite common in XML schemas). Also note that some element names have been changed. <customer> <name>James</name> <orders> <order id='1'>...</order> <order id='2'>...</order> </orders> <email-addresses> <email-address>jstrachan@apache.org</email-address> </email-addresses> </customer> Betwixt aims to provide a diversity of possible mappings such that the developer can choose, if they wish, how their beans appear as XML to support whatever XML encoding scheme that is desired. If no particular mapping is provided then Betwixt will create a default mapping for you. Also the customization mechanism allows you to just override the parts you want to and let Betwixt default the rest. So if you just want to rename a property in XML for a certain type, thats all you need to do. No need to hand-code what Betwixt can deduce for itself. Customizing the mapping of a bean to XML (basic).betwixt filesThe XMLIntrospector will look for files of the form className.betwixt on the classpath using the same ClassLoader used to load the given class and use that document to specify the mapping to XML. If this file does not exist then the default introspection rules are used. The simplest possible file may just set the name of the element. e.g. <?xml version='1.0' encoding='UTF-8' ?> <info> <element name='channel'/> <addDefaults/> </info> The above means to use the name 'channel' for the outer most element for the
given type. The <?xml version='1.0' encoding='UTF-8' ?> <info primitiveTypes='attribute'> <hide property='something'/> <element name='channel'/> <element name='customerList'> <element name='customer' property='customers'/> </element> <element name='foo'> <attribute name='qqq' property='ppp'/> <element name='bar' property='xyz'/> <addDefaults/> </element> </info> Note that the The primitiveTypes attribute in the Finally static text can be specified using a value attribute inside an
<?xml version='1.0' encoding='UTF-8' ?> <info primitiveTypes='element'> <element name='rss'/> <attribute name='version' value='0.91'/> <element name='channel'/> <addDefaults/> </element> </info> |