users@jaxb.java.net

Re: XJC frustration

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 26 Mar 2007 10:13:16 -0700

Brian Pontarelli wrote:
> Just wanted to put this out on the list because I spent a number of
> hours trying to get XJC to play nice and finally gave up in favor of
> hand coding because it just would not get with the program. I have this:
>
> <xs:complexType name="entry" mixed="true">
> <xs:sequence>
> <xs:element name="phrase" type="xs:string" minOccurs="1"
> maxOccurs="1"/>
> </xs:sequence>
> </xs:complexType>

I'm sorry to hear that XJC wasted your time.


> and no matter if I changed the type of the "phrase" element to a complex
> type, or a simple type or just used the inline schema types like this it
> would generate this code:
>
> @XmlElementRef(name = "phrase", namespace =
> "http://www.inversoft.com/schemas/profanity-1.0/database", type =
> JAXBElement.class)
> @XmlMixed
> protected List<Serializable> content;

The reason why it did this is because the complex type has a mixed
content, so XJC thought you needed to distinguish ordinary string and
the <phase> element. So it figured you need a heterogeneous list of
String and Phase element, and the best common base type it was able to
figure out was Serializable (whether XJC should ever use Serializable
has been a subject of debate --- it doesn't seem to provide any type
safety over Object.)

> This obviously sucks really bad because it doesn't provide a clean OO
> API. So, after much battling I gave up and changed it to this:
>
> @XmlElement(name = "phrase")
> private String phrase;

If you do this, this won't capture content like:

   I wonder if I can get <phase>a new machine<phase> for my work!

you'll drop everything except "a new machine".

Perhaps you didn't intend mixed="true"?

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com