2

I have a stuck with java class generation issue with following xsd. ```

<xs:element name="WsResponse" nillable="true">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="CustomerName" minOccurs="0"/>
                <xs:element ref="AccountNumber" minOccurs="0"/>
                <xs:element ref="MSISDN" minOccurs="0"/>
                <xs:element ref="TotalAmount" minOccurs="0"/>
                <xs:element ref="TaxAmount" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
</xs:element>

I'm using ant to generate java classes using com.sun.tools.xjc.XJC2Task and I'm having jaxb 2.2.4.

Issue I'm having is that generated WsResponse java class do not have the @XmlRootElement annotation.

When I remove the nillable="true" from the above xsd and try class generation, generated java class includes the @XmlRootElement annotation.

Since XSD is provided by 3rd party service I can't change that. So is there a way to have generated class to include @XmlRootElement annotation?

Xstian
  • 8,184
  • 10
  • 42
  • 72
Chathurika Sandarenu
  • 1,368
  • 13
  • 25
  • Have you tried using external global binding by `` ? – Xstian Jul 19 '15 at 11:31
  • No, `` doesn't work. – Chathurika Sandarenu Jul 20 '15 at 00:33
  • Try using this anser [LINK](http://stackoverflow.com/a/26545603/3364187) – Xstian Jul 20 '15 at 09:38
  • I tried that as well. It adds the `@XmlRootElement` to the class. But still the `ObjectFactory` is created with `JAXBElement createWsResponse(WsResponse value)`. So I get `JAXBElement` instead of `WsResponse`. Is there a way change that as well? – Chathurika Sandarenu Jul 21 '15 at 05:16
  • `JAXBElemetn` is mandatory .. see my answer [here](http://stackoverflow.com/a/31478362/3364187). In your scenario is `minOccurs=0`, because its default value. – Xstian Jul 21 '15 at 07:26
  • So that means there is nothing wrong in the class generation? When extracting the unmarshled response is it normal practice to use `JAXBIntrospector.getValue`? With that we can get the `WsResponse` object. – Chathurika Sandarenu Jul 21 '15 at 09:21
  • Yes, the generation is correct. `JAXBElement` is used in these scenarios. You could use either `JAXBIntrospector.getValue(..)` or `getValue()` within `JAXBElement` to retrieve `WsResponse`. – Xstian Jul 21 '15 at 09:34
  • Anyway if you think that the links which i reported above they were useful, I'll appreciate some up votes. – Xstian Jul 21 '15 at 09:44
  • 1
    Thanks for your replies. Sure I will :) – Chathurika Sandarenu Jul 21 '15 at 09:51

0 Answers0