【问题标题】:JAXB Root Class Rename using binding.xml使用 binding.xml 重命名 JAXB 根​​类
【发布时间】:2013-09-09 13:27:56
【问题描述】:

我使用 Eclipse 中的 Dali 插件使用 xsd 文件生成 Java 类,该文件本质上只是在模式文件上调用 xjc。我使用建议here 通过将XML 绑定文件应用于类生成来解决命名冲突。这很好用,但我试图通过重命名根元素更进一步,结果是我丢失了 XmlRootElement 注释。我尝试使用 annox 重新添加根元素注释,但出现此错误:不支持的绑定命名空间“http://annox.dev.java.net”。也许您的意思是“http://java.sun.com/xml/ns/jaxb/xjc”?

这是我最初的 binding.xml 文件(没有 annox):

<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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    version="2.1">
    <!-- Force all classes to be generated at the top level, this will potentially cause name conflicts -->
    <jaxb:globalBindings localScoping="toplevel"/>
    <jaxb:bindings schemaLocation="mySchema-1.0.0.xsd">
        <!-- Rename the root element -->
        <jaxb:bindings node="//xs:element[@name='MyRootClassNameIsReallyLong']/xs:complexType">
            <jaxb:class name="ShorterName"/>
        </jaxb:bindings>
        <!-- Rename the Bar class to resolve a naming conflict -->
        <jaxb:bindings node="//xs:element[@name='Foo']/xs:complexType/xs:sequence/xs:element[@name='Bar']/xs:complexType">
            <jaxb:class name="FooBar"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

顺便提一下,架构文件来自第三方,所以我没有兴趣修改它。同样,我宁愿不篡改生成的 Java 文件,这也是我对绑定 xml 方法感兴趣的原因。

编辑 (9/11/2013) - 这是带有 annox 的绑定 XML:

<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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    xmlns:annox="http://annox.dev.java.net"
    version="2.1">
    <!-- Force all classes to be generated at the top level, this will potentially cause name conflicts -->
    <jaxb:globalBindings localScoping="toplevel"/>
    <jaxb:bindings schemaLocation="mySchema-1.0.0.xsd">
        <!-- Rename the root element -->
        <jaxb:bindings node="//xs:element[@name='MyRootClassNameIsReallyLong']/xs:complexType">
            <jaxb:class name="ShorterName"/>
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="MyRootClassNameIsReallyLong" />
            </annox:annotate>
        </jaxb:bindings>
        <!-- Rename the Bar class to resolve a naming conflict -->
        <jaxb:bindings node="//xs:element[@name='Foo']/xs:complexType/xs:sequence/xs:element[@name='Bar']/xs:complexType">
            <jaxb:class name="FooBar"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

【问题讨论】:

  • 你能用 annox 显示你的绑定文件吗?

标签: java xml jaxb xsd


【解决方案1】:

Annox 是一个 XJC 附加组件,因此您除了声明命名空间前缀 (xmlns:annox="http://annox.dev.java.net) 外,还需要将其声明为 extensionBindingPrefix

您的开始标签应如下所示:

<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"
               xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb 
                                   http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
               jaxb:extensionBindingPrefix="annox"
               version="2.1">

【讨论】:

  • 我之前有类似的东西,但我不得不把它拿出来,因为 Eclipse 给了我以下错误:cvc-complex-type.3.2.2: Attribute 'jaxb:extensionBindingPrefix' is not allowed to出现在元素 'jaxb:bindings' 中。
  • 您是否将 xjc 编译器的 extension 选项设置为 true?
  • 如问题中所述,我正在使用 Dali Eclipse 插件来生成 XML。我所指的错误是红色波浪线的形式。你知道如何让 Eclipse 在这里快乐吗?
  • 此外,我不相信 annox 还活着并且在踢,因为在浏览器中转到 annox.dev.java.net 绝对不会返回任何结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 2013-09-06
相关资源
最近更新 更多