XLS元素在类Heirarchy中丢失

时间:2016-02-22 22:31:43

标签: java xml xsd jaxb

我在尝试实施Blaise' JAXB and Inheritance blog的代码示例时遇到了麻烦。元素" Path"在某处翻译时迷失了:我设置它然后返回null。我使用xjc将下面的代码翻译成java,创建并填充对象,将其发送到java消息总线,然后使用消息。但是,其他所有内容都按预期设置。这是我迄今为止的版本:

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

1 个答案:

答案 0 :(得分:0)

我道歉。我删除了&#34; MsgHeader&#34;从上面的问题,因为我认为它没有适用,并将Header全部放入Stack Overflow问题将是麻烦的。这对我有用:

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

有效的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>

现在我可能无法解决的问题是如何将msgHeader放在Path和Operator字段之上。