0

I'm having trouble trying to implement the code examples from Blaise's JAXB and Inheritance blog. The element "Path" is getting lost in translation somewhere: I'm setting it then it returns as null. I'm using xjc to translate the code below to java, creating and populating the object, sending it to the java message bus, then consuming the message. However, everything else is set as expected. Here's my version thus far:

<xs:schema ...>
  <xs:complexType name="vehicle" abstract="true">
    <xs:annotation>
      <xs:appinfo>
        <jaxb:class ref="other.extensions.vehicle" />
      </xs:appinfo>
    </xs:annotation>
    <xs:sequence>
      <element name="Path" type="xs:string" minOccurs="0"/> <!-- Problem Child -->
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="MyVehicle" abstract="true">
    <xs:complexContent>
      <xs:extension base="vehicle" >
        <xs:sequence>
          <element name="Operator" type="xs:string" minOccurs="0" maxOccurs="0" />
        <xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:element name="car">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="MyVehicle" >
          <xs:sequence>
            <element name="Path" type="xs:string" minOccurs="0"/>
            <element name="Operator" type="xs:string" minOccurs="0"/>
            <element name="Model" type="xs:string" minOccurs="0"/>
            <element name="DriveTrain" type="xs:string" minOccurs="0"/>
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
</xs:schema>
Everlight
  • 431
  • 5
  • 18
  • Using [Java's JAXBContext](http://stackoverflow.com/questions/7212064/is-it-possible-to-generate-a-xsd-from-a-jaxb-annotated-class) to go from my desired java objects to *.xsd files works great, but going back to java files with xjc fails with a "Unique Particle Attribution Error". – Everlight Feb 23 '16 at 18:11
  • I tried to take Path and Operator out of the complexTypes then use ` – Everlight Feb 24 '16 at 18:20

1 Answers1

0

I apologize. I removed the "MsgHeader" from the above question because I thought it didn't apply, and to put the Header in its entirety in a Stack Overflow question would be cumbersome. Here's what works for me:

<xs:schema ...>
  <xs:complexType name="MessageHeader" >
    <xs:annotation>
      <xs:appinfo>
        <jaxb:class ref="other.extensions.MsgHeader" />
      </xs:appinfo>
    </xs:annotation>
    <xs:sequence>
      <element name="Date" type="xs:date"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="vehicle" abstract="true">
    <xs:annotation>
      <xs:appinfo>
        <jaxb:class ref="other.extensions.vehicle" />
      </xs:appinfo>
    </xs:annotation>
    <xs:sequence>
      <element name="Path" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="MyVehicle" abstract="true">
    <xs:complexContent>
      <xs:extension base="vehicle" >
        <xs:sequence>
          <element name="Operator" type="xs:string" minOccurs="0" />
        <xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:element name="car">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="MyVehicle" >
          <xs:sequence>
            <element name="msgHeader" type="MessageHeader" minOccurs="0"/>
            <element name="Model" type="xs:string" minOccurs="0"/>
            <element name="DriveTrain" type="xs:string" minOccurs="0"/>
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

Valid XML:

<car>
  <Path>Going to town</Path>
  <Operator>Aunt Linda</Operator>
  <msgHeader>
    <Date>2016-02-24Z</Date>
  </msgHeader>
  <Model>Nissan</Model>
  <DriveTrain>2WD</DriveTrain>
</car>

Now the problem I probably won't be able to solve is how to get the msgHeader above the Path and Operator fields.

Everlight
  • 431
  • 5
  • 18