You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用XJC生成OGC IndoorGML的JAXB类遇属性重定义报错如何解决

问题原因

这些官方发布的XSD本身符合XML Schema规范,报错是JAXB默认的Java属性命名映射规则导致的:当XSD中存在同名的元素/属性、或者映射后生成的Java属性重名时,就会触发冲突错误,是GML、XLink这类标准XSD使用xjc生成Java类时的常见问题。

解决方法

有两种方式可以解决冲突完成类生成:

方式1:使用绑定文件(推荐,可控性高)

不需要修改原始XSD,通过自定义JAXB绑定规则重命名冲突属性即可:

  1. 新建名为bindings.xjb的配置文件,写入以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
  jaxb:extensionBindingPrefixes="xjc">

  <!-- 解决GML geometryPrimitives.xsd中的Rows冲突 -->
  <jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/geometryPrimitives.xsd" node="//xsd:complexType[@name='GridType']/xsd:sequence/xsd:element[@name='rows']">
    <jaxb:property name="gridRows"/>
  </jaxb:bindings>
  <jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/geometryPrimitives.xsd" node="//xsd:complexType[@name='GridType']/xsd:attribute[@name='rows']">
    <jaxb:property name="gridRowsAttribute"/>
  </jaxb:bindings>

  <!-- 解决xlink.xsd中的Title冲突 -->
  <jaxb:bindings schemaLocation="http://www.w3.org/1999/xlink.xsd" node="//xsd:attribute[@name='title']">
    <jaxb:property name="xlinkTitle"/>
  </jaxb:bindings>
  <jaxb:bindings schemaLocation="http://www.w3.org/1999/xlink.xsd" node="//xsd:element[@name='title']">
    <jaxb:property name="xlinkTitleElement"/>
  </jaxb:bindings>

</jaxb:bindings>
  1. 调整xjc执行命令,添加-b参数指定绑定文件:
xjc -d src -p [你的实际包名] -nv -b bindings.xjb [indoorGML核心XSD的本地路径/在线地址]

将命令中的占位符替换为你实际的包名、XSD路径即可正常生成类。

方式2:使用自动命名冲突解决参数

如果不需要自定义属性名,可以直接在xjc命令中添加-XautoNameResolution扩展参数,让xjc自动处理重名冲突:

xjc -d src -p [你的实际包名] -nv -XautoNameResolution [indoorGML核心XSD路径]

内容的提问来源于stack exchange,提问作者Fabian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 03:18:03