Mapping SchemasImplicit Mapping SchemaIf not stated otherwise, dom4jb uses an implicit mapping schema to transform an object into a XML structure. This implicit schema is very simple and primarily based on the properties of the given object. Every property of the object will be transformed into an element with the same name as the property. The element content is determined by the value of the property.
Supposed an object of the following address class should be mapped into a XML structure based on this implicit mapping schema. public class Address { private String name; private String street; private String city; public Address(String _name, String _street, String _city) { name = _name; street = _street; city = _city; } public String getCity() { return city; } public String getName() { return name; } public String getStreet() { return street; } public String toString() { return name + ", " + street + ", " + city; } } It would give you something like this: <address> <name>Michael Mueller</name> <street>Berliner Str. 12</street> <city>Berlin</Berlin> </address> Note, that the enclosing element < address > is not part of this mapping step. Rather it depends on the property of the object, which will give you this address object or is explicit defined as enclosing element. Explicit Mapping SchemaEven though the main application area of dom4jb is to provide an easy XML data binding mechanism to transform the result by means of XSLT and thus the resulting internal XML structure is of secondary importance, it's sometimes useful to influence the way a specific class will be represented. Therefor it's possible to explicitly declare the way, in which objects of a specific class should be mapped. The way to declare how objects of a specific class should be mapped is somehow similar to the way you explicitly expose the features of a JavaBean TM in a separate, associated class that implements the BeanInfo interface ( q.v. ). By associating a XMLBeanInfo class with your Bean, you can provide explicit information about the mapping between the properties of the object and the XML schema. Such a XMLBeanInfo class contain d escriptors, which define
A XMLBeanInfo class for the address class above could look e.g. as follows:
import com.teamkonzept.dom4jb.beans.XMLBeanInfo; import com.teamkonzept.dom4jb.schema.*; public class AddressXMLBeanInfo implements XMLBeanInfo { private static final GroupDescriptor CONTENT_DESCRIPTOR = new Sequence(new Groupable[] { new ElementDescriptor("name", Address.class), new ElementDescriptor("street", Address.class)}); private static final GroupDescriptor ATTRIBUTE_DESCRIPTOR = new Sequence(new Groupable[] { new AttributeDescriptor("city", Address.class)}); private static final GroupDescriptor DATA_DESCRIPTOR = new StringDescriptor(); public GroupDescriptor getContentDescriptors() { return CONTENT_DESCRIPTOR; } public GroupDescriptor getAttributeDescriptors() { return ATTRIBUTE_DESCRIPTOR; } public DataDescriptor getDataDescriptor() { return DATA_DESCRIPTOR; } public String getItemName() { return "address"; } } The use of this schema description in terms of a XMLBeanInfo class results in a XML structure like this:
<address city="Berlin"> <name>Michael Mueller</name> <street>Berliner Str. 12</street> </address> |