首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JAXB从XSD到Java的多层导入失败

使用JAXB从XSD到Java的多层导入失败
EN

Stack Overflow用户
提问于 2017-01-20 04:16:34
回答 1查看 1.1K关注 0票数 0

在使用JAXB2从XSD创建Java类时,我面临一个问题。

如果JAXB能处理这件事,请告诉我。

场景:

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

2) 项目B具有c.xsd,通过导入b.xsd使用b.xsd

c.xsd正在使用目录来查找Maven JAR中的b.xsd,这是作为依赖项添加的。

发布

项目A构建得很好,但是项目B会抛出错误,因为它无法找到b.xsd内部使用的a.xsd

误差

解析架构.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-解析:无法将名称'addme:address‘解析为(N)’元素声明‘组件。在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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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片段

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2017-01-22 08:37:35

目录文件需要重写两个xsd的位置:

代码语言:javascript
复制
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规则。每个条目的左侧是xsd中使用的systemLocation,而不是名称空间。PUBLIC规则不适用于您,因为您的xsds指定了一个systemLocation,而且在xjc中存在一个缺陷,在指定systemLocation时阻止PUBLIC规则工作。

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

参考资料:使用目录

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41756018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档