users@jaxb.java.net

Polymorphism problems

From: Matt Munz <mmunz_at_apelon.com>
Date: Sun, 27 Jun 2004 14:38:56 -0400

Hi all,

  I'm new to this list and JAXB, so please bear with me. I searched the archives, but didn't see a clear answer to this question. The following was also posted to the Java Technology & XML web forum.

 I am finding little use in JAXB's polymorphism. As such, I think I'm going to need to switch to another framework that has a more useful polymorphism feature (or else none at all).

As indicated below, I have a schema that includes a "Person" complex type, and an "Employee" complex type that extends it. There is also an element "people" that is a sequence of "Person" elements. I have used JAXB-generated classes to add instances of Person and Employee (generated classes) to an instance of the People class. The marshaller then erroneously outputs the extensions to Person found in Employee (see below).

Having seen this, I wonder what use polymorphism (in the classes generated by JAXB) has, if it is not acceptable to use a subclass wherever its superclass is allowed. Perhaps there is some other way to do polymorphism that I'm missing?

I really want to have a functionality such as the code example below implies. Does JAXB have the feature I'm looking for, or do I need to switch to another framework? If I need to switch, which one should I use?

Thanks for any help that can be offered.

schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="people">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="person" type="Person" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Person">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Employee">
<xs:complexContent>
<xs:extension base="Person">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>

expected JAXB output:

<people>
<person>
<name>Joe Sample</name>
</person>
<person>
<name>Jane Sample Employee</name>
</person>
</people>

actual JAXB output (invalid):

<people>
<person>
<name>Joe Sample</name>
</person>
<person>
<name>Jane Sample Employee</name>
<title>Engineer</title>
</person>
</people>

Java code:

Person person = factory.createPerson();
person.setName("Joe Sample");
Employee employee = factory.createEmployee();
employee.setName("Jane Sample Employee");
employee.setTitle("Engineer");
People people = factory.createPeople();
people.getPerson().add(person);
people.getPerson().add(employee);

- Matt Munz



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net