如何使用Maven从具有通用架构的多个XSD生成Java类

用户311633:

我正在尝试从多个xsd文件生成一个Java类。但是我得到这个错误。

org.xml.sax.SAXParseException; ...已经定义了“ somelement”

我认为这是由于包含多次的常见类型所致。

如何使用Maven生成Java类?

我在目录中具有以下架构:

xsd:

  • Example_QualifyRS.xsd
  • Example_QualifyRQ.xsd
  • Example_Peble_CommonTypes.xsd
  • Example_CommonTypes.xsd
  • Example_SimpleTypes.xsd

Example_QualifyRS.xsd

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EXAMPLE/2007/00" elementFormDefault="qualified" version="1.002" id="EXAMPLE2016.1">
    <xs:include schemaLocation="EXAMPLE_CommonTypes.xsd"/>
    <xs:include schemaLocation="EXAMPLE_SimpleTypes.xsd"/>
    <xs:include schemaLocation="EXAMPLE_Peble_CommonTypes.xsd"/>
         <xs:element name="someelement">...</xs:element>

Example_Peble_CommonTypes.xsd

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EXAMPLE/2007/00" elementFormDefault="qualified" version="1.000" id="EXAMPLE2016.1">
    <xs:include schemaLocation="EXAMPLE_CommonTypes.xsd"/>

Example_QualifyRQ.xsd

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EXAMPLE/2007/00" elementFormDefault="qualified" version="1.001" id="EXAMPLE2016.1">
    <xs:include schemaLocation="EXAMPLE_CommonTypes.xsd"/>
    <xs:include schemaLocation="EXAMPLE_SimpleTypes.xsd"/>
    <xs:include schemaLocation="EXAMPLE_Peble_CommonTypes.xsd"/>
        <xs:element name="someelement">...</xs:element>

这就是我如何在Maven中生成类

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>example-schema</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <xsdPathWithinArtifact>xsd</xsdPathWithinArtifact>
                            <addGeneratedAnnotation>true</addGeneratedAnnotation>
                            <laxSchemaValidation>true</laxSchemaValidation>
                            <laxSchemaValidation>true</laxSchemaValidation>
                            <readOnly>true</readOnly>
                            <verbose>true</verbose>
                            <sources>
                                <source>src/main/resources/xsd</source>
                            </sources>
                            <packageName>com.example</packageName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
马丁提斯:

假设您在common.xsd中有一个“人”,如下例所示:

<xs:schema xmlns="common.schema.def"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="common.schema.def"
           elementFormDefault="qualified">

    <xs:complexType name="Person">
        <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="age" type="xs:int"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

您想在另一个架构中使用它。然后,您将执行以下操作:

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:common="common.schema.def"
           targetNamespace="http://www.example.org/EXAMPLE/2007/00"
           elementFormDefault="qualified">
    <xs:import namespace="common.schema.def" schemaLocation="common.xsd"/>

    <xs:complexType name="someClass">
        <xs:sequence>
            <xs:element name="person" type="common:Person"/>
            <xs:element name="alias" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

编辑以回复评论:

之所以发生名称冲突,是因为您在插件中指定了程序包名称。然后,尝试将具有相同名称的类放在同一包下。让我们从插件中删除软件包名称,并使用绑定为每个架构创建所需的软件包。

您的插件如下所示:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>example-schema</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration>
                <xsdPathWithinArtifact>xsd</xsdPathWithinArtifact>
                <addGeneratedAnnotation>true</addGeneratedAnnotation>
                <laxSchemaValidation>true</laxSchemaValidation>
                <laxSchemaValidation>true</laxSchemaValidation>
                <readOnly>true</readOnly>
                <verbose>true</verbose>
                <sources>
                    <source>src/main/resources/xsd</source>
                </sources>
                <xjbSources>
                    <xjbSource>src/main/resources/xjb</xjbSource>
                </xjbSources>
            </configuration>
        </execution>
    </executions>
</plugin>

并且bindings.xjb看起来像这样:

<jxb:bindings version="1.0"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings schemaLocation="../xsd/common.xsd">
        <jxb:schemaBindings>
            <jxb:package name="common.packagename"/>
        </jxb:schemaBindings>
    </jxb:bindings>
    <jxb:bindings schemaLocation="../xsd/someothercommon.xsd">
        <jxb:schemaBindings>
            <jxb:package name="othercommon.packagename"/>
        </jxb:schemaBindings>
    </jxb:bindings>
</jxb:bindings>

为了测试它,我使用了另一个xsd和一个名为Person的元素,看起来像这样:

<xs:schema xmlns="othercommon.schema.def"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="othercommon.schema.def"
           elementFormDefault="qualified">

    <xs:complexType name="Person">
        <xs:sequence>
            <xs:element name="alias" type="xs:string"/>
            <xs:element name="id" type="xs:int"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

同时使用这两种模式的架构将更新为:

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:common="common.schema.def"
           xmlns:othercommon="othercommon.schema.def"
           targetNamespace="http://www.example.org/EXAMPLE/2007/00"
           elementFormDefault="qualified">
    <xs:import namespace="common.schema.def" schemaLocation="common.xsd"/>
    <xs:import namespace="othercommon.schema.def" schemaLocation="someothercommon.xsd"/>

    <xs:complexType name="someClass">
        <xs:sequence>
            <xs:element name="person" type="common:Person"/>
            <xs:element name="otherpersontype" type="othercommon:Person"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

具有相同嵌套 xsd 类生成的多个 Xsd

具有多个类的Java通用通配符

使用Cayenne从具有多个架构的Postgress数据库生成类

从XSD架构生成类

如何在gradle中使用xmlbeans从xsd生成Java类

如何编写具有通用类类型的Java方法?

使用Roaster,如何生成具有特定通用类型的接口?

具有通用Java的类型参数类

如何使用Gradle从WSDL和XSD生成类,等效于maven-jaxb2-plugin

如何使用dask / fastparquet从多个目录中读取多个Parquet文件(具有相同架构)

从具有相似属性名称的XSD生成JAXB类

如何通过maven-jaxb从XSD仅生成所需的类

不是使用Maven-jaxb2-plugin从xsd生成类的?

JAXB Maven使用多个情节生成类

如何使通用类对多个实体使用JPARepository

如何配置axis2-java2wsdl-maven-plugin使用多个类来生成一个wsdl?

jQuery ui滑块具有通用类的多个滑块事件

如何使用jsviews刷新具有相似类的多个元素

如何使用 Automapper 映射到具有多个实现的类

如何使用azure复制活动加载具有重叠但动态架构列的多个文件?

如何定义具有通用参数的类来扩展具有通用参数的类?

如何使用Java创建多个架构连接?

使用xsd.exe从架构生成的类不包含枚举值

如何使用Java从XSD生成XML数据?

如何从具有不同架构的多个资源中摄取数据

如何在生成的 JAXB 类中构造 XSD 以具有原始包装器而不是原始类型?

如何使用JAXB从xsd生成一个实现Serializable接口的Java类?

使用 Keras 创建具有多个输入的 NN 架构

从带有leve包的xsd文件生成Java类