Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding an annotation to a JAXB binding class from a schema

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 generated class: what I want to do is to specify that a element can be generated as a root element before the code generation.

I use external JAXB customizations (.xjb files). The purpose is here to not modifying the schemas (as they are defining standards).

Anybody knows how do that? Thanks!

NJ

like image 904
njames Avatar asked Jan 02 '12 15:01

njames


People also ask

What is @XmlType annotation in Java?

If class is annotated with @XmlType(name="") , it is mapped to an anonymous type otherwise, the class name maps to a complex type name. The XmlName() annotation element can be used to customize the name. Properties and fields that are mapped to elements are mapped to a content model within a complex type.

What is XJB file in JAXB?

Generally we create a bindings file with . xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

What is the name of the tool JAXB used for binding?

JAXB provides the xjc schema compiler tool, the schemagen schema generator tool, and a runtime framework. The xjc schema compiler tool enables you to start with an XML schema definition (XSD) to create a set of JavaBeans that map to the elements and types defined in the XSD schema.

What is @XmlRootElement?

Annotation Type XmlRootElementMaps a class or an enum type to an XML element. Usage. The @XmlRootElement annotation can be used with the following program elements: a top level class. an enum type.


1 Answers

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

like image 151
njames Avatar answered Sep 18 '22 07:09

njames