users@jaxb.java.net

Re: Customization question

From: Joe Fialli <joseph.fialli_at_sun.com>
Date: Mon, 04 Nov 2002 18:03:29 -0500

Noel Rappin wrote:
> I have an XML document that looks something like this (simplified, of
> course...):
>
> <parent>
> <child name="fred"/>
> <child name="barney"/>
> <child name="wilma"/>
> </parent>
>
> What I get out of JAXB is a Parent interface that has a List property
> Child. This is fine. However, when the item is marshaled, I'd also
> like to have the Child objects be inserted into a Map, where the name
> attribute is the key, and the Child object is the value.

Noel,

 From the XML document example above, I assume you have a schema
with an element <child> with "maxOccurs > 1".

The <child> element is mapped to a collection property named "child".
JAXB does not support customizing a collection property to a
java.util.Map at this time. One can customize the collection property
to be an JavaBean indexed bean or an implementation of a java.util.List
class. The rationale behind this is that ordering within a XML document
is significant and only collection types that implement java.util.List
are capable of preserving sequential order between elements of the
collection.

If you were allowed to specify that the "collectionType" for property
"child" was an implementation of "java.util.Map" and that one of the
"child"'s property was the key for the Map, the property for @name in
your example above, the Java representation of the <child> collection
property as a Map would not be capable of preserving the original XML
document order between the <child> elements when marshalling the content
back to XML. Maps are not capable of supporting arbitrary ordering
between elements, a sorted map can preserve a natural sorted order
between keys at best.

In this intial version of data binding, we have tried to be careful to
not allow customizations that would make it impossible to preserve the
orignal XML document within the Java representation.

-Joe Fialli, Sun Microsystems


>
> I've been trying to figure out how to get JAXB to bind to a custom class
> that implements the parent interface and that will do this, but don't
> think I've quite figured it out. Is there a way to get JAXB to do what
> I want?
>
> Thanks,
>
> Noel Rappin


--