java - XJC - 编译器无法接受此类定制

标签 java maven xsd jaxb

我想从我的 Java 项目调用 ISAN Restful API,所以我尝试使用 maven-jaxb2-plugin 从 xsd 文件生成 java bean。这是 xsds:

我下载了这些文件并将它们复制到我的 src/main/resources 文件夹中并定义了一个目录。 当我构建项目时,出现错误,因为两种类型具有相同的名称:

org.xml.sax.SAXParseExceptionpublicId: http://www.isan.org/schema/v1.11/common/language; systemId: http://www.isan.org/schema/v1.11/common/language.xsd; lineNumber: 39; columnNumber: 48; A class/interface with the same name "org.isan.CodingSystemType" is already in use. Use a class customization to resolve this conflict.
org.xml.sax.SAXParseExceptionpublicId: http://www.isan.org/schema/v1.11/common/country; systemId: http://www.isan.org/schema/v1.11/common/country.xsd; lineNumber: 39; columnNumber: 48; (Relevant to above error) another "CodingSystemType" is generated from here.

这是正确的:language.xsd 和 country.xsd 都定义了一个名为 CodingSystemType 的类型:

    <xs:simpleType name="CodingSystemType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="ISO639_2"/>
            <xs:enumeration value="RFC3066"/>
        </xs:restriction>
    </xs:simpleType> 

    <xs:simpleType name="CodingSystemType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="ISO3166_1"/>
        </xs:restriction>
    </xs:simpleType> 

按照建议,我尝试对 country.xsd 类型使用类自定义。我在 pom.xml 中添加了这个绑定(bind):

<bindings>
    <binding>
        <url>http://www.isan.org/schema/v1.11/common/country.xjb</url>
    </binding>
</bindings>

xjc 文件:

<bindings version="2.0" xmlns="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:annox="http://annox.dev.java.net"
    xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
    <bindings schemaLocation="http://www.isan.org/schema/v1.11/common/country.xsd">

        <bindings node="//xs:simpleType[@name='CodingSystemType']">
        <class name="CountryCodingSystemType" />
        </bindings>

    </bindings>
</bindings>

现在,我遇到另一个我无法处理的错误:

[ERROR] Error while parsing schema(s).Location [ http://www.isan.org/schema/v1.11/common/country.xjb{7,58}].
com.sun.istack.SAXParseException2; systemId: http://www.isan.org/schema/v1.11/common/country.xjb; lineNumber: 7; columnNumber: 58; compiler was unable to honor this class customization. It is attached to a wrong place, or its inconsistent with other bindings.
[ERROR] Error while parsing schema(s).Location [ http://www.isan.org/schema/v1.11/common/country.xsd{39,48}].
com.sun.istack.SAXParseException2; systemId: http://www.isan.org/schema/v1.11/common/country.xsd; lineNumber: 39; columnNumber: 48; (the above customization is attached to the following location in the schema)

最佳答案

尝试

<bindings node="//xs:simpleType[@name='CodingSystemType']">
    <typesafeEnumClass name="CountryCodingSystemType" />
</bindings>

相反。

我认为 XJC 使自定义枚举和普通类有所不同。查看相关问题:

关于java - XJC - 编译器无法接受此类定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468120/

相关文章:

java - 另一个类的 JPanel 不会出现在主 JFrame 中

java - 尝试在 Windows 上运行 Maven 时出错

Eclipse gradle 和 maven 工具 - 本地解析

java - 是否可以使用spark-avro数据源创建Spark应用程序并通过 'java -jar'执行它

当节点有多个子节点时,针对 XSD 的 XML 验证失败

java - 将对象从 Spring HandlerInterceptor 传递到 Servlet Filter

java - "pre-casting"与每次类型转换的效率

java - 如何重现 Java OutOfMemoryError - 超出 GC 开销限制

c# - XDocument 更改模式

xsd - 在 XML Schema 中为不同的命名空间声明一个属性