【发布时间】: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 显示你的绑定文件吗?