users@jaxb.java.net

Re: _at_XmlRootElement repeats elements in Schema

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Sat, 25 Oct 2008 09:05:12 +0200

This generic method wraps an arbitrary element of some type T into a
JAXBElement<T> which can then be marshalled.

<T> JAXBElement<T> wrap( String ns, String tag, T o ){
    QName qtag = new QName( ns, tag );
    Class<?> clazz = o.getClass();
    @SuppressWarnings( "unchecked" )
    JAXBElement<T> jbe = new JAXBElement( qtag, clazz, o );
    return jbe;
}

Its application is simple:

Marshaller m = ...;
SomeType st = ...;
JAXBElement<SomeType> jbx = wrap( "http://www.acme.com", "someTag", st );
m.marshal( jbx, System.out );

You may not be able to validate the resulting XML against a schema without a
suitable element definition.

Wolfgang Laun

On Fri, Oct 24, 2008 at 9:56 PM, Alex Sherwin
<alex.sherwin_at_acadiasoft.com>wrote:

> Hello,
> I've got some classes which can all be marshalled individually, and as such
> all have an @XmlRootElement annotation.
>
> However, when I want to use these classes as part of a larger object, i.e.
> "MessageWrapper" which has an @XmlRootElement annotation and contains three
> List<> properties, each of which is a list of a JAXB annotated class that
> has an @XmlRootElement.
>
> Technically this all works just fine, but if I generate the schema for
> MessageWrapper, the messageWrapper element is not the only root element of
> the schema. All JAXB @XmlRootElement annotated objects that get marshalled
> at some point (even though they are all properties of the class I'm
> generating the Schema for) are also root elements in the resulting schema.
>
> If I remove the @XmlRootElement annotation from all classes which are being
> used within MessageWrapper, the resulting schema is as expected (only one
> messageWrapper) root element.
>
> How can I make these elements not be root elements of MessageWrapper's
> Schema, but can still be individually marshalled?
>
> --
> Alexander Sherwin
>