问题
I would like to generate the Java classes SignResponse
and AuthResponse
from the following XSD using XJC:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="SignResponse" type="tns:OrderResponseType"/>
<xsd:element name="AuthResponse" type="tns:OrderResponseType"/>
<xsd:complexType name="OrderResponseType">
<xsd:sequence>
<xsd:element name="orderRef" type="xsd:string"/>
<xsd:element name="autoStartToken" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
This means generating classes from elements with same complexType. Using the above XSD as input, XJC will generate an OrderResponseType
class, but no SignResponse
and AuthResponse
. It seems type="tns:OrderResponseType"
is not used properly by XJC, because when OrderResponseType
is defined inside SignResponse
and AuthResponse
, everything works OK:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="SignResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="orderRef" type="xsd:string"/>
<xsd:element name="autoStartToken" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="AuthResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="orderRef" type="xsd:string"/>
<xsd:element name="autoStartToken" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Using the following XJB, type="tns:OrderResponseType"
is actually used, but this works only when there is only one element that has type="tns:OrderResponseType"
as attribute:
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<jxb:globalBindings>
<xjc:simple/>
</jxb:globalBindings>
</jxb:bindings>
Using multiple elements in combination with this <xjc:simple/>
XJB binding element will cause only OrderResponseType
to be generated.
Using custom bindings like:
<jxb:bindings node="//xs:complexType[@name='OrderResponseType']">
<jxb:class name="SignResponse"/>
</jxb:bindings>
also works only for one element. XJC will throw an exception when using e.g.:
<jxb:bindings node="//xs:complexType[@name='OrderResponseType']">
<jxb:class name="SignResponse"/>
</jxb:bindings>
<jxb:bindings node="//xs:complexType[@name='OrderResponseType']">
<jxb:class name="AuthResponse"/>
</jxb:bindings>
Thanks in advance.
回答1:
Probably not the best solution, because SignResponse
's and AuthResponse
's base class will be JAXBElement<OrderResponseType>
, not OrderResponseType
. But you can have two different classes using the following jaxb bindings:
<jxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
<jxb:bindings node="//xsd:element[@name='SignResponse']">
<jxb:class name="SignResponse"/>
</jxb:bindings>
<jxb:bindings node="//xsd:element[@name='AuthResponse']">
<jxb:class name="AuthResponse"/>
</jxb:bindings>
</jxb:bindings>
来源:https://stackoverflow.com/questions/43399861/jaxb-xjc-generate-classes-from-elements-with-same-complextype