views:

223

answers:

2

Using Castor, how do you map Java class java.util.ArrayList to element <ArrayList/> instead of <array-list/> while still including the elements that it contains?

For example, the class mapping

<class name="java.util.ArrayList">
    <map-to xml="ArrayList" />
</class>

maps an ArrayList object to an empty element, omitting elements for the objects that the ArrayList may contain. How do you coerce Castor into also generating elements for those contained objects?

+1  A: 

Perhaps this can help: http://www.castor.org/how-to-map-a-collection.html

Maybe this one? http://www.castor.org/how-to-wrap-a-collection-with-a-wrapper-element.html

Ash Kim
This is not the same case as the one in question. The container in this case is not an attribute in a user defined class, but is a stand-alone `java.util.ArrayList` which contains object instances of a user-defined class. Using the mapping that I gave in my question, Castor renames the element that it generates for `java.util.ArrayList` from `<array-list/>` to `<ArrayList/>`, but it does not generate elements for the objects in the array. It generates an empty `<ArrayList/>` element.
Derek Mahar
This closer to what I need, but again, in my case, an `ArrayList` is the root container and should explicitly appear as the root element in the output. I'll try to alter this mapping to get what I require.
Derek Mahar
+1  A: 

Add attribute autocomplete="true" to the mapping:

<class name="java.util.ArrayList" auto-complete="true">
    <map-to xml="ArrayList" />
</class>

See description of auto-complete under Section <class/> in Castor XML Mapping.

Derek Mahar
auto-complete aye - nice one.
Ash Kim
I wonder, why isn't `auto-complete=true` the default?
Derek Mahar