users@jaxb.java.net

Re: Duplicate element on marshall: element as type

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Thu, 16 Aug 2007 22:05:24 +0200

Hi.

> Search as I might, I cannot find an answer to the following:
>
> I have a schema of the form:
>
> <xsd:complexType name="FooType">
> <xsd:element name="Name" type="xsd:string"/>
> </xsd:complexType>
> <xsd:element name="Foo" type="tns:FooType"/>
> <xsd:complexType name="BarType">
> <xsd:element name="Foo" type="tns:FooType"/>
> </xsd:complexType>
> <xsd:element name="Bar" type="tns:BarType"/>
>
> Everything works fine in terms of compiling and unmarshalling.
>
> Now I add an instance of FooImpl to a BarImpl instance, which works as
> FooImpl extends FooType, and
> I try and marshall and I get:
>
> <Bar>
> <Foo>
> <Foo>
> <Name>name</Name>
> </Foo>
> </Foo>
> </Bar>
>
> Any ideas as to how fix?

I guess it's JAXB 1 related.

You have to add instances of FooTypeImpl, not of FooImpl. I also
experienced this problem and found out that this was the only
solution.

I also use the "copyable" add-on from jaxbcommons. This addon
generates "copy" methods so I often do something like:

final FooType newFoo = new FooTypeImpl();
// oldFoo is a Foo extends FooType
oldFoo.copyTo(newFoo);
bar.getFoo().add(newFoo);

Bye.
/lexi