commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cliche Sylvain <>
Subject [betwixt] Map content serialization
Date Wed, 09 Mar 2005 22:14:32 GMT
Whenever I try to convert the DTO_Test object that contains a Map into a XML
String the display looks like this (see test program below):


However, if the method setWriteEmptyElements(true) is called over the
BeanWriter object instance with a value of true for its parameter:
BeanWriter writer = new BeanWriter(outputWriter);

then the display looks like this (the content of the Map is actually

So I'm a little surprised to have to set to true this method in order to
serialize the content of a Map. Here is a fragment of the JavaDoc from the
method AbstractBeanWriter .setWriteEmptyElements(boolean writeEmptyElements)
and nothing is mentionned regarding the serialization of the content of a
	Sets whether empty elements should be written into the output.
	An empty element is one that has no attributes, no child elements
and no body text. For example, <element/> is an empty element but <element
attr='value'/> is not. 
Also, the downside with this approach is that all empty fields from the
objects get serialized as well, which adds useless data (I don't need all
the empty elements I just need the content of a Map). Is there a way to
serialize the content of a Map without having to set to true the method
setWriteEmptyElements ? I am using Betwixt 0.6 with WSAD 5.1 in a J2EE 1.3
environment. The test program and most objects are written below.



 * Object which hold data to be exported to xml
public class DTO_Test {
	public DTO_Test() { super(); }	
	private Map addresses = new HashMap();
	public Map getAddresses() { return addresses; }	
	public void setAddresses(Map newAddresses) { this.addresses =
newAddresses; }	
	public void addAddress(String name, Object bla) {
addresses.put(name, bla); }

 * Actual test program
public class ObjectSerialization {
	public static final void main(String [] bla) {
		DTO_Test dto = new DTO_Test();
		dto.addAddress("key_A", "value_A");
		dto.addAddress("key_B", "value_B");
		XmlUtilTest xmlUtil = new XmlUtilTest();

		try {
			String strObj = xmlUtil.toXml(dto);
			System.out.println("String: \n" + strObj);
		catch(Exception ex) {
			System.out.println(">>>> Exception: " +

 * Contains methods to call in order to perform the serialization
public class XmlUtilTest {
	private static BindingConfiguration configuration =
		new BindingConfiguration(new CustomObjectStringConverter(),
	 * Transform object content to xml
	public String toXml(Object object) throws Exception {
		if (object == null) { return ""; }

		StringWriter outputWriter = new StringWriter();
		BeanWriter writer = new BeanWriter(outputWriter);
		* Must set to true to have the content of a Map serialized
		* However by doing so all empty fields get serialized as
empty elements (<example/>)
writer.getXMLIntrospector().getConfiguration().setAttributeNameMapper( new
writer.getXMLIntrospector().getConfiguration().setElementNameMapper( new

  		String name = object.getClass().getName();
  		name = name.substring(name.lastIndexOf(".") + 1);
		writer.write(name , object);
		return outputWriter.toString();

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message