users@jaxb.java.net

Re: How to force JAXB to generate correct output for derived classes

From: Sergey Beryozkin <sergey.beryozkin_at_iona.com>
Date: Tue, 21 Jul 2009 09:08:37 -0700 (PDT)

thanks Wolfgang. It's one possible option.

IMHO, it can still be a nice enhancement, if JAXBElement<BaseType>
createBase(BaseType value )
were used if DerivedType/BaseType had no @XmlRootElement. For ex, an
original schema might have no element declarations at all, a user then goes
and adds

JAXBElement<BaseType> createBase(BaseType value )

only. So when DerivedType instance is being serialized, the runtime
eventually finds this method.
Perhaps, it is not that simple in the reality though :-)

Sergey


Wolfgang Laun-2 wrote:
>
> Let me go back. The original schema had
>
> <xs:element name="base" type="BaseType"/>
> <xs:element name="derived" type="DerivedType"/>
>
> and the Object factory contains create methods for JAXBElement according
> to
> this:
> JAXBElement<BaseType> createBase(BaseType value )
> JAXBElement<DerivedType> createDerived(DerivedType value)
>
> The resulting XML is OK in both cases, it will create <base>...</base> or
> <derived>...</derived>.
>
> You cannot define another schema element with name="base" with DerivedType
> to tell JAXB that you want this, too.
>
> -W
>
> On Tue, Jul 21, 2009 at 5:02 PM, Sergey Beryozkin
> <sergey.beryozkin_at_iona.com
>> wrote:
>
>>
>> OK.
>>
>> Here's some class :
>>
>> public SomeClass {
>> public BaseType get() { return new DerivedType(); }
>> }
>>
>> where BaseType and DerivedType have no @XmlRootElement;
>>
>> Now, you suggested earlier on to just go ahead and write a simply utility
>> class which would wrap BaseType in a JAXBElement, using BaseType.class as
>> a
>> declared type. I'll do it but what I'm asking is why, given that
>> ObjectFactory has a method creating a JAXBElement<BaseType>, I have to do
>> what ObjectFactory can do anyway ?
>>
>> Does it explain clearly enough what I mean under 'pick up' ? I'd rather
>> you
>> answer the question instead :-)
>>
>> thanks for your help, Sergey
>>
>>
>>
>>
>>
>>
>>
>>
>> Wolfgang Laun-2 wrote:
>> >
>> > Then I misunderstood your original question.
>> >
>> > On Tue, Jul 21, 2009 at 4:13 PM, Sergey Beryozkin
>> > <sergey.beryozkin_at_iona.com
>> >> wrote:
>> >
>> >>
>> >> I don't quite follow it.
>> >>
>> >> I'm simply asking why the method which has already been generated and
>> is
>> >> available on the ObjectFactory is ignored,
>> >
>> >
>> > The methods in the ObjectFactory may be used by
>> > - an application composing a content tree that will finally be
>> marshalled
>> > to
>> > XML; ignoring or not ignoring these methods is up to the application's
>> > author;
>> > - JAXB for creating a content tree during unmarshalling from XML; and
>> here
>> > they shouldn't be ignored by JAXB (but we weren't discussing
>> unmarshalling
>> > anyway)
>> >
>> >
>> >
>> >> what is the point of generating
>> >> such methods ?
>> >>
>> >> I'm also asking when such ObjectFactory JAXBElement getters are being
>> >> used
>> >> ?
>> >> Consider a set of classes being generated from a schema, like the one
>> >> Karen
>> >> provided. It's not always possible to go and add @XmlRootElement to
>> every
>> >> generated class.
>> >
>> >
>> > You shouldn't have to change generated classes - what makes you think
>> you
>> > have to?
>> >
>> >
>> >> So I thought that is where ObjectFactory would help, as
>> >> guess what, it already has all the JAXBElement getters generated.
>> >>
>> >
>> > It contains methods for creating objects of the generated classes (from
>> > complexType) and
>> > for JAXBElement<?>, typically required when you have <xs:element...> in
>> > your
>> > schema.
>> >
>> >
>> >>
>> >> So why is it that such a getter is not picked up ?
>> >
>> >
>> > What do you mean by "pick up"? ("To pick up" means to take a thing
>> that's
>> > lying around.)
>> > Who should "pick up"?
>> >
>> > I'm about to provide to
>> >> provide a generic JAXBElement wrapper facility anyway but I'd like to
>> >> understand why ObjectFactory is not being utilized
>> >>
>> >> cheers, Sergey
>> >>
>> >>
>> >> Wolfgang Laun-2 wrote:
>> >> >
>> >> > On Tue, Jul 21, 2009 at 3:29 PM, Sergey Beryozkin
>> >> > <sergey.beryozkin_at_iona.com
>> >> >> wrote:
>> >> >
>> >> >>
>> >> >> Hi
>> >> >>
>> >> >> thanks. Can you explain please why a similar method in
>> ObjectFactory
>> >> is
>> >> >> not
>> >> >> picked up ?
>> >> >
>> >> >
>> >> > Because tailors sell you a suit and not cloth, needle and yarn ;-)
>> >> >
>> >> > Seriously now: the generated code is supposed the regular case of a
>> >> schema
>> >> > binding.
>> >> > If you want to add all sorts of extensions - where do you draw the
>> >> line?
>> >> >
>> >> > -W
>> >> >
>> >> >
>> >> >>
>> >> >> The generated factory does have a method returning
>> >> JAXBElement<BaseType>.
>> >> >> I thought that if I remove @XmlRootElement on BaseType/DerivedType
>> >> then
>> >> >> this
>> >> >> method will be picked up, but it's not, JAXB does expect
>> DerivedType
>> >> to
>> >> >> have
>> >> >> @XmlRootElement, when DerivedType is serialized directly.
>> >> >>
>> >> >> Do such ObjectFactory methods used only when class members of List
>> >> >> type/etc
>> >> >> are being serialized ?
>> >> >> cheers, Sergey
>> >> >>
>> >> >>
>> >> >> Wolfgang Laun-2 wrote:
>> >> >> >
>> >> >> > With this simple method
>> >> >> >
>> >> >> > <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;
>> >> >> > }
>> >> >> >
>> >> >> > you do
>> >> >> > DerivedType doc = of.createDerivedType();
>> >> >> > JAXBElement<DerivedType> jbe = wrap( "", "base", doc );
>> >> >> >
>> >> >> > and you'll get
>> >> >> >
>> >> >> > <base xsi:type="DerivedType" xmlns:xsi="
>> >> >> > http://www.w3.org/2001/XMLSchema-instance">
>> >> >> > ...
>> >> >> > </base>
>> >> >> >
>> >> >> > -W
>> >> >> >
>> >> >> > On Tue, Jul 21, 2009 at 2:21 PM, Qzpmwo <qzpmwo_at_gmail.com> wrote:
>> >> >> >
>> >> >> >>
>> >> >> >> As I understand, since I did not specify substitution group
>> correct
>> >> >> xml
>> >> >> >> should be:
>> >> >> >>
>> >> >> >> <base xsi:type="derivedType>...</base>
>> >> >> >>
>> >> >> >> Let me know if my understanding of XSD/XML is wrong.
>> >> >> >>
>> >> >> >> Thanks
>> >> >> >> --- Karen
>> >> >> >>
>> >> >> >>
>> >> >> >> Wolfgang Laun-2 wrote:
>> >> >> >> >
>> >> >> >> > What do you see that is wrong?
>> >> >> >> > -W
>> >> >> >> >
>> >> >> >> >
>> >> >> >> >
>> >> >> >> > On Mon, Jul 20, 2009 at 7:05 PM, Qzpmwo <qzpmwo_at_gmail.com>
>> wrote:
>> >> >> >> >
>> >> >> >> >>
>> >> >> >> >> Hi,
>> >> >> >> >>
>> >> >> >> >> I'm having a problem with derived schema types and XML
>> generated
>> >> by
>> >> >> >> JAXB.
>> >> >> >> >>
>> >> >> >> >> Basically I'm having a schema like:
>> >> >> >> >>
>> >> >> >> >> <xs:schema>
>> >> >> >> >>
>> >> >> >> >> <xs:complexType name="BaseType">
>> >> >> >> >> <xs:sequence>
>> >> >> >> >> </xs:sequence>
>> >> >> >> >> </xs:complexType>
>> >> >> >> >>
>> >> >> >> >> <xs:complexType name="DerivedType">
>> >> >> >> >> <xs:complexContent>
>> >> >> >> >> <xs:extension base="BaseType">
>> >> >> >> >> <xs:sequence>
>> >> >> >> >> </xs:sequence>
>> >> >> >> >> </xs:extension>
>> >> >> >> >> </xs:complexContent>
>> >> >> >> >> </xs:complexType>
>> >> >> >> >>
>> >> >> >> >> <xs:complexType name="ListType">
>> >> >> >> >> <xs:sequence>
>> >> >> >> >> <xs:element name="item" type="BaseType"
>> >> >> minOccurs="0"
>> >> >> >> >> maxOccurs="unbounded" />
>> >> >> >> >> </xs:sequence>
>> >> >> >> >> </xs:complexType>
>> >> >> >> >>
>> >> >> >> >> <xs:element name="base" type="BaseType" />
>> >> >> >> >> <xs:element name="derived" type="DerivedType" />
>> >> >> >> >> <xs:element name="list" type="ListType" />
>> >> >> >> >>
>> >> >> >> >> </xs:schema>
>> >> >> >> >>
>> >> >> >> >> XJC is used to generate java classes from this schema.
>> >> >> >> >>
>> >> >> >> >> 1) @XmlSeeAlso pointing to DerivedType class and
>> >> @XmlRootElement
>> >> >> >> >> annotations have been added to BaseType java class code
>> >> >> >> >> 2) @XmlRootElement has been added to DerivedType java class
>> code
>> >> >> >> >> 3) @XmlRootElement has been added to ListType java class code
>> >> >> >> >>
>> >> >> >> >> Right xml is produced when I'm using ListType
>> >> >> >> >>
>> >> >> >> >> <list>
>> >> >> >> >> <base xsi:type="DerivedType">...</base>
>> >> >> >> >> ...
>> >> >> >> >> </list>
>> >> >> >> >>
>> >> >> >> >> But when I'm trying to get single object of the DerivedType,
>> I
>> >> see
>> >> >> >> wrong
>> >> >> >> >> xml
>> >> >> >> >> (since I'm not using substitution group):
>> >> >> >> >>
>> >> >> >> >> <derived>...</derived>
>> >> >> >> >>
>> >> >> >> >> So question: Is there a way to force JAXB return correct xml?
>> >> >> >> >>
>> >> >> >> >> Thanks in advance
>> >> >> >> >> --- Karen
>> >> >> >> >> --
>> >> >> >> >> View this message in context:
>> >> >> >> >>
>> >> >> >>
>> >> >>
>> >>
>> http://www.nabble.com/How-to-force-JAXB-to-generate-correct-output-for-derived-classes-tp24573728p24573728.html
>> >> >> >> >> Sent from the java.net - jaxb users mailing list archive at
>> >> >> >> Nabble.com.
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >>
>> >> >>
>> ---------------------------------------------------------------------
>> >> >> >> >> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> >> >> >> >> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >
>> >> >> >> >
>> >> >> >>
>> >> >> >> --
>> >> >> >> View this message in context:
>> >> >> >>
>> >> >>
>> >>
>> http://www.nabble.com/How-to-force-JAXB-to-generate-correct-output-for-derived-classes-tp24573728p24586576.html
>> >> >> >> Sent from the java.net - jaxb users mailing list archive at
>> >> >> Nabble.com.
>> >> >> >>
>> >> >> >>
>> >> >> >>
>> >> ---------------------------------------------------------------------
>> >> >> >> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> >> >> >> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>> >> >> >>
>> >> >> >>
>> >> >> >
>> >> >> >
>> >> >>
>> >> >> --
>> >> >> View this message in context:
>> >> >>
>> >>
>> http://www.nabble.com/How-to-force-JAXB-to-generate-correct-output-for-derived-classes-tp24573728p24587677.html
>> >> >> Sent from the java.net - jaxb users mailing list archive at
>> >> Nabble.com.
>> >> >>
>> >> >>
>> >> >>
>> ---------------------------------------------------------------------
>> >> >> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> >> >> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/How-to-force-JAXB-to-generate-correct-output-for-derived-classes-tp24573728p24588492.html
>> >> Sent from the java.net - jaxb users mailing list archive at
>> Nabble.com.
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> >> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/How-to-force-JAXB-to-generate-correct-output-for-derived-classes-tp24573728p24589439.html
>> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>
>

-- 
View this message in context: http://www.nabble.com/How-to-force-JAXB-to-generate-correct-output-for-derived-classes-tp24573728p24590707.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.