繁体   English   中英

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

[英]XSD to Java using JAXB with multi-level import failing

使用JAXB2从XSD创建Java类时遇到问题。

请让我知道JAXB是否可以处理此问题。

场景:

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

2) 项目Bc.xsd并使用b.xsd通过导入b.xsd
c.xsd使用目录在Maven JAR中查找b.xsd ,并将其作为依赖项添加。

问题

项目A会很好,但项目B抛出错误,因为它找不到a.xsd在内部使用b.xsd。

错误

[错误]解析模式时出错。位置[ 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)“元素声明”组件。 在com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)处com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)

项目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>

格式

<?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

格式

<?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 ::!“

投影一个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>

您的目录文件需要重写两个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规则。 每个条目的左侧是systemLocation使用的systemLocation,而不是名称空间。 PUBLIC规则将不会为你工作,因为你的XSD指定systemLocation ,并有在XJC防止错误PUBLIC规则从当工作systemLocation指定的。

还要检查您的xsds是否已复制到已发布jar的根目录中,并且其名称与目录中使用的名称匹配。

参考: 使用目录

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM