从模式向JAXB绑定类添加注释

nja*_*mes 9 java annotations jaxb

嗨stackoverflow世界,

我想在XSD中指定JAXB可以将特定元素用作XmlRootElement.

我知道如何将注释添加到生成的类中:我想要做的是指定在代码生成之前可以将元素生成为根元素.

我使用外部JAXB自定义(.xjb文件).目的是不修改模式(因为它们是定义标准).

谁知道那是怎么回事?谢谢!

NJ

nja*_*mes 17

问题解决了.

JAXB插件Annotate http://confluence.highsource.org/display/J2B/Annotate+Plugin完成这项工作.

在jaxb绑定文件中添加以下片段(外部绑定,即.xjb文件):

<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>
Run Code Online (Sandbox Code Playgroud)

不要忘记声明命名空间:

<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>
Run Code Online (Sandbox Code Playgroud)

并使用ANT或MAVEN任务http://confluence.highsource.org/display/J2B/User+Guide来继续生成源代码.

我仍然搜索如何手动指定(没有使用ant或maven的xjc任务)JAXB扩展,但它现在可以工作.(我有自己的ANT脚本,这就是我搜索手动调用XJC的原因).

JAXB扩展机制非常方便,看看JAXB2基础知识:http://confluence.highsource.org/display/J2B/Home

  • 如何添加@something({@ some("a"),@ some("b")})等注释? (2认同)