从模式向 JAXB 绑定类添加注释 [英] adding an annotation to a JAXB binding class from a schema

查看:35
本文介绍了从模式向 JAXB 绑定类添加注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

stackoverflow 世界,

Hi stackoverflow world,

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

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.

我使用外部 JAXB 自定义(.xjb 文件).此处的目的是不修改架构(因为它们正在定义标准).

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!

新泽西

推荐答案

问题已解决.

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

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

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

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>

并使用 ANT 或 MAVEN 任务 http://confluence.highsource.org/display/J2B/User+指导进行源的生成.

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

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

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).

JAXB扩展机制很方便,看看JAXB2 Basics:http://confluence.highsource.org/display/J2B/Home

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

这篇关于从模式向 JAXB 绑定类添加注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆