JAXB根类使用binding.xml重命名

时间:2013-09-09 13:27:57

标签: java xml jaxb xsd

我使用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>

1 个答案:

答案 0 :(得分:0)

Annox是一个XJC添加,因此除了声明命名空间prefx(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">