Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...