users@jaxb.java.net

Re: How do I separate the <xsd:choice/> sub elements into individual list properties?

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Fri, 12 Aug 2011 17:24:01 +0200

On 12 August 2011 16:51, Jarrod Roberson <jarrod_at_vertigrated.com> wrote:

>
> What if I don't care about the order?
>
> This isn't for you to decide - the author of the XML schema appeared to
care. I know that this is slightly paradox, because such an XML declaration
fits both: an arbitrary and an ordered inhomogeneous sequence. Clearly, XML
Schema does not let you distinguish.

Given that you don't care about the order then an inhomogeneous list is
either easier for the marshaller who does not use JAXB (just add one child
after the other) when it is mixed, or easier for the unmarshaller, when
elements are sequenced by category.


> I don't understand what you are talking about List<JAXBElement/>?
> Everything comes back as a plain Object and I have to test for instanceof to
> separate out the types?
>

Given

    static <T> List<T> extract( List<JAXBElement<?>> jbList, Class<T> clazz
){
        List<T> list = new ArrayList<T>();
        for( JAXBElement<?> jbel: jbList ){
            Object value = jbel.getValue();
        if( clazz.isInstance( value ) ){
        list.add( clazz.cast( value ) );
        }
    }
        return list;
    }

you can do

   List<Navitem> navitems =
    extract( navmap.getNavitemOrDebug(), Navitem.class );

-W