adding an annotation to a JAXB binding class from a schema

前端 未结 1 1988
后悔当初
后悔当初 2020-12-03 15:23

Hi stackoverflow world,

I want to specify in a XSD that a specific element can be used as a XmlRootElement by JAXB.

I know how to add the annotation to the g

相关标签:
1条回答
  • Problem solved.

    The JAXB plugin Annotate http://confluence.highsource.org/display/J2B/Annotate+Plugin do the job.

    Add the following fragment in your jaxb binding file (external binding, i.e. a .xjb file):

    <jaxb:bindings schemaLocation="csw/2.0.2/CSW-discovery.xsd" node="/xs:schema">
      <jaxb:bindings node="xs:complexType[@name='GetRecordsType']">
        <annox:annotate>
      <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                     name="GetRecordsType" />
    </annox:annotate>
      </jaxb:bindings>
    </jaxb:bindings>
    

    Do not forget to declare the namespaces:

    <jaxb:bindings 
      xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
      xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xmlns:annox="http://annox.dev.java.net"
      xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
      jaxb:extensionBindingPrefixes="xjc annox" version="2.1">
     ...
     </jaxb:bindings>
    

    And use a ANT or MAVEN task http://confluence.highsource.org/display/J2B/User+Guide to proceed the generation of the sources.

    I still search how to specify manually (without an xjc task with ant or maven) the JAXB extensions but it works now. (I have my own ANT script what's why I search to manually call XJC).

    The JAXB extension mechanism is very convenient, have a look to JAXB2 Basics: http://confluence.highsource.org/display/J2B/Home

    0 讨论(0)
提交回复
热议问题