2017-01-20 162 views
0

我在使用JAXB2從XSD創建Java類時遇到了一個問題。使用帶有多級導入失敗JAXB的XSD到Java

請讓我知道,如果JAXB可以處理這個。

場景:

1)項目Aa.xsdb.xsd用不同的名稱空間 b.xsd使用a.xsd與導入名稱空間標籤。

2)項目B具有c.xsd並通過導入b.xsd使用b.xsd
c.xsd正在使用目錄在Maven JAR中查找作爲依賴項添加的b.xsd

ISSUE

項目A建立罰款,但項目B拋出錯誤,因爲它找不到a.xsd在內部使用b.xsd。

ERROR

[ERROR]錯誤在解析模式(一個或多個).Location [http://www.example.com/test2/test2.xsd {15,39}]。 org.xml.sax.SAXParseException; systemId:http://www.example.com/test2/test2.xsd; lineNumber:15; columnNumber:39; src-resolve:無法將名稱'addme:address'解析爲(n)'元素聲明'組件。 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java: 134)

項目A

a.xsd

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.com/address" xmlns="http://www.example.com/address" 
    elementFormDefault="qualified"> 
    <xs:element name="address"> 

     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="street" type="xs:string" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 

b.xsd

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.com/test2" 
    xmlns="http://www.example.com/test2" 
    xmlns:addme="http://www.example.com/address" 
    elementFormDefault="qualified"> 

    <xs:import namespace="http://www.example.com/address" schemaLocation="http://www.example.com/address/address.xsd"/> 

    <xs:element name="test2"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="addme:address" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 

項目B

c.xsd

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema targetNamespace="http://www.example.com/customer" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.com/customer" 
    xmlns:test="http://www.example.com/test2" elementFormDefault="qualified"> 

    <xs:import namespace="http://www.example.com/test2" 
     schemaLocation="http://www.example.com/test2/test2.xsd" /> 

    <xs:element name="customer"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="test:test2" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 

目錄文件的B項目

REWRITE_SYSTEM「http://www.example.com/test2」「maven:com.test.projectA:projectA:jar ::!「

項目A POM片斷

<?xml version="1.0"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

    <properties> 
     <xsd.build.dir>${basedir}/src/main/resources</xsd.build.dir> 
     <generated.source.location>${basedir}/target/generated-sources/src</generated.source.location> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.jvnet.jaxb2_commons</groupId> 
      <artifactId>jaxb2-basics</artifactId> 
      <version>0.6.4</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.jvnet.jaxb2.maven2</groupId> 
       <artifactId>maven-jaxb2-plugin</artifactId> 
       <version>0.13.1</version> 
       <executions> 
        <execution> 
         <goals> 
          <goal>generate</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <generateDirectory>${generated.source.location}</generateDirectory> 
        <schemaDirectory>${xsd.build.dir}</schemaDirectory> 
        <episode>true</episode> 
        <addIfExistsToEpisodeSchemaBindings>true</addIfExistsToEpisodeSchemaBindings> 
        <plugins> 
         <plugin> 
          <groupId>org.jvnet.jaxb2_commons</groupId> 
          <artifactId>jaxb2-basics</artifactId> 
          <version>0.6.4</version> 
         </plugin> 
        </plugins> 
        <args> 
         <arg>-XtoString</arg> 
         <arg>-Xequals</arg> 
         <arg>-XhashCode</arg> 
        </args> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-source-plugin</artifactId> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>jar</goal> 
          <goal>test-jar</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>attach-sources</id> 
         <phase>DISABLE_FORKED_LIFECYCLE_MSOURCES-13</phase> 
        </execution> 
       </executions> 
      </plugin> 

     </plugins> 

    </build> 
</project> 

**PROJECT B POM** 

     <?xml version="1.0"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" .> 

    <properties> 
     <xsd.build.dir>${basedir}/src/main/resources</xsd.build.dir> 
     <generated.source.location>${basedir}/target/generated-sources/src</generated.source.location> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>com.test.projectA</groupId> 
      <artifactId>projectA</artifactId> 
      <version>0.0.1-SNAPSHOT</version> 
     </dependency> 
     <dependency> 
      <groupId>org.jvnet.jaxb2_commons</groupId> 
      <artifactId>jaxb2-basics</artifactId> 
      <version>0.6.4</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.jvnet.jaxb2.maven2</groupId> 
       <artifactId>maven-jaxb2-plugin</artifactId> 
       <version>0.13.1</version> 
       <executions> 
        <execution> 
         <goals> 
          <goal>generate</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <generateDirectory>${generated.source.location}</generateDirectory> 
        <schemaDirectory>${xsd.build.dir}</schemaDirectory> 
        <catalog>src/main/resources/catalog.cat</catalog> 
        <useDependenciesAsEpisodes>true</useDependenciesAsEpisodes> 
        <plugins> 
         <plugin> 
          <groupId>org.jvnet.jaxb2_commons</groupId> 
          <artifactId>jaxb2-basics</artifactId> 
          <version>0.6.4</version> 
         </plugin> 
        </plugins> 
        <args> 
         <arg>-XtoString</arg> 
         <arg>-Xequals</arg> 
         <arg>-XhashCode</arg> 
        </args> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-source-plugin</artifactId> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>jar</goal> 
          <goal>test-jar</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>attach-sources</id> 
         <phase>DISABLE_FORKED_LIFECYCLE_MSOURCES-13</phase> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 

    </build> 
</project> 
+0

我不能看到'HTTP目錄條目:// www.example.com/address'。它可能試圖從_b.xsd_中指定的位置加載它,該位置是「http:// www.example.com/address/address.xsd」。 – teppic

+0

謝謝@teppic。其實我之前沒有碰到過,我也這麼想過。基本上當我建立projecA有一個和B xsd,建立良好,問題出現在項目中有c xsd - >引用b xsd - >引用xsd。 a和b是作爲依賴關係提供的,可能是它在解析時無法從「c」級獲得「a」,但不確定是否有任何特定的方法來告知系統或目錄中的數據... – Dave

回答

0

您的目錄文件需要重寫兩個XSD文件的位置:

REWRITE_SYSTEM "http://www.example.com/test2/test2.xsd" "maven:com.test.projectA:projectA:jar::!/b.xsd" 
REWRITE_SYSTEM "http://www.example.com/address/address.xsd" "maven:com.test.projectA:projectA:jar::!/a.xsd" 

注意,這些都是重寫規則,不PUBLIC規則。每個條目的左側是在xsds中使用的systemLocation,而不是名稱空間。PUBLIC規則不適用於您,因爲您的x sds指定一個systemLocation,並且xjc中存在一個錯誤,當指定systemLocation時,阻止PUBLIC規則生效。

同時檢查您的xsds是否被複制到發佈jar的根目錄,並且它們的名稱與目錄中使用的名稱相匹配。

參考:Using-Catalogs

+0

如果我使用正如你提到的完整路徑,它也不會找到b.xsd。 「http://www.example.com/test2/test2.xsd」「maven:com.test.projectA:projectA:jar ::!/ b.xsd」它只是像我之前所做的那樣「http ://www.example.com/test2「」maven:com.test.projectA:projectA:jar ::!「我的意思是隻有命名空間沒有xsd – Dave

+0

我已經檢查過,這與您的示例代碼(包括目錄中發佈的Maven項目和Maven網址)一起工作。檢查您的系統位置與目錄中的url完全匹配,並且xsds已被複制到已發佈的jar到cataolog中指定的路徑。 – teppic