获取错误在eclipse中使用maven-jaxb2-plugin时,元素绑定的名称空间必须来自模式名称空间

时间:2014-12-05 11:06:40

标签: eclipse jaxb xsd xjc maven-jaxb2-plugin

我有不同的.xsd文件,我想在不同的包中从它们生成java类。我将maven maven-jaxb2-plugin配置为以下

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.11.0</version>
    <executions>
        <execution>
            <id>generate-messages</id>
                <goals>
                    <goal>generate</goal>
                </goals>
        </execution>
    </executions>
    <configuration>
        <forceRegenerate>true</forceRegenerate>
        <generateDirectory>${basedir}/src/main/java</generateDirectory>
        <schemas>
            <schema>
                <fileset>  
                    <directory>${basedir}/src/main/resources/bindings</directory>   
                    <includes>                                                      
                        <include>lmsApiBinding.xml</include>    
                    </includes>                              
                </fileset>
            </schema>
        </schemas>
    </configuration>
</plugin>

这是我的档案

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="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"
    xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb 
                    http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    version="2.1">

    <jaxb:globalBindings generateElementProperty="false">
        <xjc:serializable uid="1" />
    </jaxb:globalBindings>

    <jaxb:bindings schemaLocation="/src/main/webapp/schemas/lmsapi/serviceoperations/CustomerServiceOperations.xsd" node="/xsd:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="abc.customer" />
        </jaxb:schemaBindings>
    </jaxb:bindings>

    <jaxb:bindings schemaLocation="/src/main/webapp/schemas/lmsapi/serviceoperations/EnrollmentServiceOperations.xsd" node="/xsd:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="def.enrollment" />
        </jaxb:schemaBindings>
    </jaxb:bindings>
    ....
</jaxb:bindings>

这是我的CustomerServiceOperations.xsd文件

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://customer.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns="http://customer.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" 
    xmlns:cust="http://customer.types.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns:tr="http://transactionresult.types.lmsapi.message.webservice.lms.vu360.softech.com"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

    <xsd:import namespace="http://transactionresult.types.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="../types/TransactionResultType.xsd"/>
    <xsd:import namespace="http://customer.types.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="../types/Customer.xsd"/>

    <xsd:element name="AddCustomerRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Customers" type="cust:Customers" minOccurs="1" maxOccurs="1" nillable="false" />
            </xsd:sequence>
            <xsd:attribute name="key" type="xsd:string" use="required" />
            <xsd:attribute name="ResellerId" type="xsd:nonNegativeInteger" use="required" />
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="AddCustomerResponse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="RegisterCustomers" type="cust:RegisterCustomers" minOccurs="0" maxOccurs="1" nillable="false" />
            </xsd:sequence>
            <xsd:attribute name="transactionResult" type="tr:TransactionResultType" use="required"/>
            <xsd:attribute name="transactionResultMessage" type="xsd:string"/>
        </xsd:complexType>
    </xsd:element>

</xsd:schema>

但是当我点击我的.pom文件并选择Run As --> maven Generate Sources然后我收到以下错误

org.xml.sax.SAXParseException; systemId: file:/D:/Basit/eclipse-jee-luna/workspace/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml; lineNumber: 10; columnNumber: 19; s4s-elt-schema-ns: The namespace of element 'bindings' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.
...
org.xml.sax.SAXParseException; systemId: file:/D:/Basit/eclipse-jee-luna/workspace/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml; lineNumber: 10; columnNumber: 19; s4s-elt-invalid: Element 'bindings' is not a valid element in a schema document.
...
org.xml.sax.SAXParseException; systemId: file:/D:/Basit/eclipse-jee-luna/workspace/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml; lineNumber: 10; columnNumber: 19; schema_reference.4: Failed to read schema document 'file:/D:/Basit/eclipse-jee-luna/workspace/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

为什么我收到这些错误?我做错了什么?

由于

------ ----编辑

<schemas>
    <schema>
        <fileset>
            <directory>${basedir}/src/main/webapp/schemas/lmsapi</directory>
            <includes>
                <include>/serviceoperations/CustomerServiceOperations.xsd</include>
                <include>/serviceoperations/EnrollmentServiceOperations.xsd</include>
                <include>/serviceoperations/OrgGroupServiceOperations.xsd</include>
                ...
                <include>/types/Address.xsd</include>
                <include>/types/Course.xsd</include>
                <include>/types/Customer.xsd</include>
                ....
                <include>/types/UserGroup.xsd</include>
                <include>/types/Vu360User.xsd</include>
            </includes>
        </fileset>
    </schema>
</schemas>

- 最终工作代码 -

您好,   谢谢。终于搞定了。这是我的档案LmsApiServiceOperations.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://com/softech/vu360/lms/webservice/message/lmsapi/serviceoperations" 
    xmlns="http://com/softech/vu360/lms/webservice/message/lmsapi/serviceoperations"  
    elementFormDefault="qualified">

    <xsd:import namespace="http://trainingplan.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="TrainingPlanServiceOperations.xsd"/>
    <xsd:import namespace="http://customer.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="CustomerServiceOperations.xsd"/>
    <xsd:import namespace="http://user.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="UserServiceOperations.xsd"/>
    <xsd:import namespace="http://enrollment.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="EnrollmentServiceOperations.xsd"/>
    <xsd:import namespace="http://securityroles.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="SecurityRoleServiceOperations.xsd"/>
    <xsd:import namespace="http://orggroup.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="OrgGroupServiceOperations.xsd"/>
    <xsd:import namespace="http://usergroup.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" schemaLocation="UserGroupServiceOperations.xsd"/>

</xsd:schema>

这是插件配置

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.11.0</version>
    <executions>
        <execution>
            <id>generate-messages</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <bindingDirectory>${basedir}/src/main/resources/bindings</bindingDirectory>
                <bindingIncludes>
                    <bindingInclude>lmsApiBinding.xml</bindingInclude>
                </bindingIncludes>
                <generateDirectory>${basedir}/src/main/java/</generateDirectory>
                <schemas>
                    <schema>
                        <fileset>
                            <directory>${basedir}/src/main/webapp/schemas/lmsapi/</directory>
                            <includes>
                                <include>serviceoperations/LmsApiServiceOperations.xsd</include>
                            </includes>
                        </fileset>
                    </schema>
                </schemas>
                <strict>true</strict>
                <extension>true</extension>
                <verbose>true</verbose>
                <forceRegenerate>true</forceRegenerate>
            </configuration>
        </execution>
    </executions>
</plugin>

此外,我的lmsApiBinding.xml文件现在只包含

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="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"
    xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb 
                    http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    version="2.1">

    <jaxb:globalBindings generateElementProperty="false">
        <xjc:serializable uid="1" />
    </jaxb:globalBindings>

</jaxb:bindings>

因为如果我使用类似

的东西
<jaxb:globalBindings generateElementProperty="false">
    <xjc:serializable uid="1" />
</jaxb:globalBindings>

<jaxb:bindings schemaLocation="CustomerServiceOperations.xsd" node="/xsd:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="com.softech.vu360.lms.webservice.message.lmsapi.serviceoperations.customer" />
    </jaxb:schemaBindings>
</jaxb:bindings>

<jaxb:bindings schemaLocation="/schemas/lmsapi/serviceoperations/EnrollmentServiceOperations.xsd" node="/xsd:schema">
    <jaxb:schemaBindings>
        <jaxb:package name="com.softech.vu360.lms.webservice.message.lmsapi.serviceoperations.enrollment" />
    </jaxb:schemaBindings>
</jaxb:bindings>

然后选择Maven - &gt;生成来源。然后我收到以下错误

com.sun.istack.SAXParseException2; systemId: file:/D:/Basit/eclipse-jee-luna/workspace/360Training/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml; lineNumber: 17; columnNumber: 86; "file:/D:/Basit/eclipse-jee-luna/workspace/360Training/lms-api-1/src/main/resources/bindings/CustomerServiceOperations.xsd" is not a part of this compilation. Is this a mistake for "file:/D:/Basit/eclipse-jee-luna/workspace/360Training/lms-api-1/src/main/resources/bindings/lmsApiBinding.xml"?

我使用的是绑定文件,因为我想在不同的包中生成类。但似乎它从targetnamespace生成包名。正如我在CustomerServiceOperations.xsd文件中声明的那样

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://customer.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns="http://customer.serviceoperations.lmsapi.message.webservice.lms.vu360.softech.com" 
    xmlns:cust="http://customer.types.lmsapi.message.webservice.lms.vu360.softech.com"
    xmlns:tr="http://transactionresult.types.lmsapi.message.webservice.lms.vu360.softech.com"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">
    ....
</xsd:schema>

因此在生成源之后。它会自动将CustomerServiceOperations.xsd操作放入包com.softech.vu360.lms.webservice.message.lmsapi.serviceoperations.customer

由于

1 个答案:

答案 0 :(得分:1)

你放了lmsApiBinding.xml(绑定文件)而不是你的XSD CustomerServiceOperations.xsd ...我在我的项目中使用这个配置并且工作正常。

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.11.0</version>
            <executions>
                <execution>
                    <id>generate-messages</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <bindingDirectory>${basedir}/src/main/resources/bindings</bindingDirectory>
                        <bindingIncludes>
                            <bindingInclude>lmsApiBinding.xml</bindingInclude>
                        </bindingIncludes>
                        <generateDirectory>${basedir}/src/main/java/</generateDirectory>
                        <schemas>
                            <schema>
                                <fileset>
                                    <directory>${basedir}/src/main/resources/schema/xsd</directory>
                                    <includes>
                                        <include>CustomerServiceOperations.xsd</include>
                                    </includes>
                                </fileset>
                            </schema>
                        </schemas>
                        <strict>true</strict>
                        <extension>true</extension>
                        <verbose>true</verbose>
                        <forceRegenerate>true</forceRegenerate>
                    </configuration>
                </execution>
            </executions>
        </plugin>