使用JAXB的XSD到Java,多级导入失败

时间:2017-01-20 04:16:34

标签: java maven xsd jaxb2

我在使用JAXB2从XSD创建Java类时遇到了一个问题。

如果JAXB能够解决这个问题,请告诉我。

方案:

1)项目A a.xsdb.xsd具有不同的名称空间 b.xsd使用带有导入名称空间标记的a.xsd

2)项目B c.xsd并导入b.xsd使用b.xsd
c.xsd正在使用目录来查找作为依赖项添加的Maven JAR中的b.xsd

问题

项目A 构建正常,但项目B 会引发错误,因为它无法找到b.xsd内部使用的a.xsd

错误

  

[ERROR]解析模式时出错。位置[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)           在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&#34; http://www.example.com/test2&#34; &#34;行家:com.test.projectA:了projectA:罐子::&#34;

投放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>

1 个答案:

答案 0 :(得分:0)

您的目录文件需要重写两个xsds:

的位置
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规则对您不起作用,因为您的xsds指定了systemLocation,并且xjc中存在一个错误,导致PUBLIC规则在指定systemLocation时无效。< / p>

同时检查您的xsds是否被复制到已发布jar的根目录,并且它们的名称与目录中使用的名称相匹配。

参考:Using-Catalogs