users@jaxb.java.net

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

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Tue, 21 Jul 2009 16:01:13 +0200

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
>
>