java - 在 java bean 类中使用 JAXB (xjc) 进行转换

标签 java

我的问题是: 我必须从头开始实现 Web 要素服务服务器,我想从 XSD 获得精确的信息:

h**p://schemas.opengis.net/wfs/1.1.0/wfs.xsd

我运行命令:

xjc h**p://schemas.opengis.net/wfs/1.1.0/wfs.xsd

[ERROR] Property "Title" is already defined. Use <jaxb:property> to resolve this conflict.
  line 232 of h**p://www.w3.org/1999/xlink.xsd

[ERROR] The following location is relevant to the above error
  line 219 of h**p://www.w3.org/1999/xlink.xsd

[ERROR] Property "Title" is already defined. Use <jaxb:property> to resolve this conflict.
  line 261 of h**p://www.w3.org/1999/xlink.xsd

[ERROR] The following location is relevant to the above error
  line 246 of h**p://www.w3.org/1999/xlink.xsd

Failed to parse a schema.

但是,当我运行时:

xjc h**p://schemas.opengis.net/ows/1.1.0/owsGetCapabilities.xsd 

该过程 100% 成功。

h**p://www.w3.org/1999/xlink.xsd 的片段部分是:

    <xs:attributeGroup name="locatorAttrs">
  <xs:attribute ref="xlink:type" fixed="locator" use="required"/>
  <xs:attribute ref="xlink:href" use="required"/>
  <xs:attribute ref="xlink:role"/>
  <xs:attribute ref="xlink:title"/>
  <xs:attribute ref="xlink:label">
   <xs:annotation>
    <xs:documentation>
     label is not required, but locators have no particular
     XLink function if they are not labeled.
    </xs:documentation>
   </xs:annotation>
  </xs:attribute>
 </xs:attributeGroup>

 <xs:group name="locatorModel">
  <xs:sequence>
   <xs:element ref="xlink:title" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
 </xs:group>

 <xs:complexType name="locatorType">
  <xs:group ref="xlink:locatorModel"/>
  <xs:attributeGroup ref="xlink:locatorAttrs"/>
 </xs:complexType>

 <xs:element name="arc" type="xlink:arcType" abstract="true"/>

 <xs:attributeGroup name="arcAttrs">
  <xs:attribute ref="xlink:type" fixed="arc" use="required"/>
  <xs:attribute ref="xlink:arcrole"/>
  <xs:attribute ref="xlink:title"/>
  <xs:attribute ref="xlink:show"/>
  <xs:attribute ref="xlink:actuate"/>
  <xs:attribute ref="xlink:from"/>
  <xs:attribute ref="xlink:to">
   <xs:annotation>
    <xs:documentation>
     from and to have default behavior when values are missing
    </xs:documentation>
   </xs:annotation>
  </xs:attribute>
 </xs:attributeGroup>

 <xs:group name="arcModel">
  <xs:sequence>
   <xs:element ref="xlink:title" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
 </xs:group>

怎么了?

最佳答案

问题出在 xmlns:xlink="http://www.w3.org/1999/xlink"

解决方法: 创建和使用 binding.xjb:

<jxb:bindings version="2.0"
      xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
      xmlns:xs="http://www.w3.org/2001/XMLSchema" >

    <jxb:bindings schemaLocation="http://www.w3.org/1999/xlink.xsd" node="/xs:schema">

        <jxb:bindings node="//xs:attributeGroup[@name='locatorAttrs']">
            <jxb:bindings node=".//xs:attribute[@ref='xlink:title']">
                <jxb:property name="title1"/>
            </jxb:bindings>
        </jxb:bindings>

        <jxb:bindings node="//xs:attributeGroup[@name='arcAttrs']">           
            <jxb:bindings node=".//xs:attribute[@ref='xlink:title']">
                <jxb:property name="title2"/>
            </jxb:bindings>            
        </jxb:bindings>              
    </jxb:bindings>          

</jxb:bindings>

工作 ant 任务(build.xml 和 binding.xjb 在同一个文件夹中):

<xjc schema="${src.folder.xsd}\${src.file.xsd}" package="${src.package}" destdir="${src.folder}" binding="binding.xjb">
        <produces dir="${src.folder}\${src.package}" includes="**/*.java" />
</xjc>

关于java - 在 java bean 类中使用 JAXB (xjc) 进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18467478/

相关文章:

java - Devtool 拒绝在框架中显示 "My uri",因为它将 'X-Frame-Options' 设置为 'DENY'

java - 区分TCP IP中的数据包

java - Apache Camel SNMP 组件

java - 在单个 Java 类的命令行中使用 JUnit 编译和运行测试

Java 断言,与单元测试和异常相比/对比

java - 使用 Object<?> 参数创建函数

java - Gson 枚举序列化

java - 由entrySet()返回的Set上的contains()和remove()的行为

java - 在Java上的Redis Hash中列出

java - 使用 API(Java?)在 Facebook 粉丝页面上发布状态