使用XJC或xmlBeans生成代理类失败

时间:2013-01-16 20:58:47

标签: java jaxb xjc amazon-mws

我正在尝试使用XJC 2.2.4-2和剧集文件生成代理类。最初的剧集生成使用amazon.xsd运行良好,但随后使用该剧集的请求失败。

我也尝试了xmlbeans scomp命令,它也失败了。

我正在运行的命令是:

xjc.exe -extension -b amazon.episode amzn-base.xsdscomp -src amazonGeneratedClasses -out feedsApi.jar new new是包含所有xsd文件的目录。

哪个输出:

[XJC]

parsing a schema...
[ERROR] SCD "AmazonOnly" didnt match any schema component
  line 69 of file:/C:/Users/roberth/Programming_Projects/new/amazon.episode

[ERROR] SCD "Amazon-Vendor-Only" didnt match any schema component
  line 60 of file:/C:/Users/roberth/Programming_Projects/new/amazon.episode

Failed to parse a schema.

第60行:

 <jaxb:bindings scd="Amazon-Vendor-Only">
      <jaxb:class ref="amazon.AmazonVendorOnly"/>
    </jaxb:bindings>

第69行:

  <jaxb:bindings scd="Amazon-Only">
      <jaxb:class ref="amazon.AmazonOnly"/>
    </jaxb:bindings>

文件amazon.AmazonVendorOnly和amazon.AmazonOnly确实存在。

[XmlBeans的]

MerchantListingsReport.xsd:27:8: error: Invalid QName value: Can't resolve prefix 'tns'
amzn-header.xsd:17:5: error: sch-props-correct.2: Duplicate global element: Header (Original global element found in file: aces.xsd)
AmazonACES.xsd:109:2: error: sch-props-correct.2: Duplicate global element: Aspiration (Original global element found in file: aces.xsd)
AmazonACES.xsd:118:2: error: sch-props-correct.2: Duplicate global element: BaseVehicle (Original global element found in file: aces.xsd)
AmazonACES.xsd:127:2: error: sch-props-correct.2: Duplicate global element: BedLength (Original global element found in file: aces.xsd)
AmazonACES.xsd:136:2: error: sch-props-correct.2: Duplicate global element: BedType (Original global element found in file: aces.xsd)
AmazonACES.xsd:145:2: error: sch-props-correct.2: Duplicate global element: BodyNumDoors (Original global element found in file: aces.xsd)
AmazonACES.xsd:154:2: error: sch-props-correct.2: Duplicate global element: BodyType (Original global element found in file: aces.xsd)
AmazonACES.xsd:163:2: error: sch-props-correct.2: Duplicate global element: BrakeABS (Original global element found in file: aces.xsd)
AmazonACES.xsd:172:2: error: sch-props-correct.2: Duplicate global element: BrakeSystem (Original global element found in file: aces.xsd)
AmazonACES.xsd:181:2: error: sch-props-correct.2: Duplicate global element: CylinderHeadType (Original global element found in file: aces.xsd)
AmazonACES.xsd:190:2: error: sch-props-correct.2: Duplicate global element: DisplayOrder (Original global element found in file: aces.xsd)
AmazonACES.xsd:191:2: error: sch-props-correct.2: Duplicate global element: DriveType (Original global element found in file: aces.xsd)
AmazonACES.xsd:200:2: error: sch-props-correct.2: Duplicate global element: EngineBase (Original global element found in file: aces.xsd)
AmazonACES.xsd:209:2: error: sch-props-correct.2: Duplicate global element: EngineDesignation (Original global element found in file: aces.xsd)
AmazonACES.xsd:218:2: error: sch-props-correct.2: Duplicate global element: EngineMfr (Original global element found in file: aces.xsd)
AmazonACES.xsd:227:2: error: sch-props-correct.2: Duplicate global element: EngineVIN (Original global element found in file: aces.xsd)
AmazonACES.xsd:236:2: error: sch-props-correct.2: Duplicate global element: EngineVersion (Original global element found in file: aces.xsd)
AmazonACES.xsd:252:2: error: sch-props-correct.2: Duplicate global element: FrontBrakeType (Original global element found in file: aces.xsd)
AmazonACES.xsd:261:2: error: sch-props-correct.2: Duplicate global element: FrontSpringType (Original global element found in file: aces.xsd)
AmazonACES.xsd:270:2: error: sch-props-correct.2: Duplicate global element: FuelDeliverySubType (Original global element found in file: aces.xsd)
AmazonACES.xsd:279:2: error: sch-props-correct.2: Duplicate global element: FuelDeliveryType (Original global element found in file: aces.xsd)
AmazonACES.xsd:288:2: error: sch-props-correct.2: Duplicate global element: FuelSystemControlType (Original global element found in file: aces.xsd)
AmazonACES.xsd:297:2: error: sch-props-correct.2: Duplicate global element: FuelSystemDesign (Original global element found in file: aces.xsd)
AmazonACES.xsd:306:2: error: sch-props-correct.2: Duplicate global element: FuelType (Original global element found in file: aces.xsd)
AmazonACES.xsd:323:2: error: sch-props-correct.2: Duplicate global element: IgnitionSystemType (Original global element found in file: aces.xsd)
AmazonACES.xsd:332:2: error: sch-props-correct.2: Duplicate global element: Make (Original global element found in file: aces.xsd)
AmazonACES.xsd:341:2: error: sch-props-correct.2: Duplicate global element: MfrBodyCode (Original global element found in file: aces.xsd)
AmazonACES.xsd:351:2: error: sch-props-correct.2: Duplicate global element: MfrLabel (Original global element found in file: aces.xsd)
AmazonACES.xsd:360:2: error: sch-props-correct.2: Duplicate global element: Model (Original global element found in file: aces.xsd)
AmazonACES.xsd:369:2: error: sch-props-correct.2: Duplicate global element: Note (Original global element found in file: aces.xsd)
AmazonACES.xsd:387:2: error: sch-props-correct.2: Duplicate global element: Part (Original global element found in file: aces.xsd)
AmazonACES.xsd:396:2: error: sch-props-correct.2: Duplicate global element: PartType (Original global element found in file: aces.xsd)
AmazonACES.xsd:405:2: error: sch-props-correct.2: Duplicate global element: Position (Original global element found in file: aces.xsd)
AmazonACES.xsd:414:2: error: sch-props-correct.2: Duplicate global element: Qty (Original global element found in file: aces.xsd)
AmazonACES.xsd:415:2: error: sch-props-correct.2: Duplicate global element: RearBrakeType (Original global element found in file: aces.xsd)
AmazonACES.xsd:424:2: error: sch-props-correct.2: Duplicate global element: RearSpringType (Original global element found in file: aces.xsd)
AmazonACES.xsd:433:2: error: sch-props-correct.2: Duplicate global element: Region (Original global element found in file: aces.xsd)
AmazonACES.xsd:451:2: error: sch-props-correct.2: Duplicate global element: SteeringSystem (Original global element found in file: aces.xsd)
AmazonACES.xsd:460:2: error: sch-props-correct.2: Duplicate global element: SteeringType (Original global element found in file: aces.xsd)
AmazonACES.xsd:469:2: error: sch-props-correct.2: Duplicate global element: SubModel (Original global element found in file: aces.xsd)
AmazonACES.xsd:508:2: error: sch-props-correct.2: Duplicate global element: TransmissionBase (Original global element found in file: aces.xsd)
AmazonACES.xsd:517:2: error: sch-props-correct.2: Duplicate global element: TransmissionControlType (Original global element found in file: aces.xsd)
AmazonACES.xsd:526:2: error: sch-props-correct.2: Duplicate global element: TransmissionMfr (Original global element found in file: aces.xsd)
AmazonACES.xsd:535:2: error: sch-props-correct.2: Duplicate global element: TransmissionMfrCode (Original global element found in file: aces.xsd)
AmazonACES.xsd:544:2: error: sch-props-correct.2: Duplicate global element: TransmissionNumSpeeds (Original global element found in file: aces.xsd)
AmazonACES.xsd:553:2: error: sch-props-correct.2: Duplicate global element: TransmissionType (Original global element found in file: aces.xsd)
AmazonACES.xsd:562:2: error: sch-props-correct.2: Duplicate global element: VehicleType (Original global element found in file: aces.xsd)
AmazonACES.xsd:571:2: error: sch-props-correct.2: Duplicate global element: WheelBase (Original global element found in file: aces.xsd)
AmazonACES.xsd:580:2: error: sch-props-correct.2: Duplicate global element: Years (Original global element found in file: aces.xsd)
PIES_5.0_02_01_2006.xsd:4548:2: error: sch-props-correct.2: Duplicate global type: MaintenanceType (Original global type found in file: AmazonPIES.xsd
)
PIES_5.0_02_01_2006.xsd:4591:2: error: sch-props-correct.2: Duplicate global type: ImageStyle (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:4664:2: error: sch-props-correct.2: Duplicate global type: FileType (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:4706:2: error: sch-props-correct.2: Duplicate global type: ImageResolution (Original global type found in file: AmazonPIES.xsd
)
PIES_5.0_02_01_2006.xsd:4756:2: error: sch-props-correct.2: Duplicate global type: ImageRepresentation (Original global type found in file: AmazonPIES
.xsd)
PIES_5.0_02_01_2006.xsd:4799:2: error: sch-props-correct.2: Duplicate global type: ColorMode (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:4846:2: error: sch-props-correct.2: Duplicate global type: Background (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:4894:2: error: sch-props-correct.2: Duplicate global type: OrientationView (Original global type found in file: AmazonPIES.xsd
)
PIES_5.0_02_01_2006.xsd:4977:2: error: sch-props-correct.2: Duplicate global type: ImageFileEncodeType (Original global type found in file: AmazonPIES
.xsd)
PIES_5.0_02_01_2006.xsd:5065:2: error: sch-props-correct.2: Duplicate global type: LanguageCode (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:5113:2: error: sch-props-correct.2: Duplicate global type: LinkCode (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:5215:2: error: sch-props-correct.2: Duplicate global type: ItemIDQualifier (Original global type found in file: AmazonPIES.xsd
)
PIES_5.0_02_01_2006.xsd:5270:2: error: sch-props-correct.2: Duplicate global type: KitsUOM (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:5384:2: error: sch-props-correct.2: Duplicate global type: PackageUOM (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:5432:2: error: sch-props-correct.2: Duplicate global type: PackageDimUOM (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:5475:2: error: sch-props-correct.2: Duplicate global type: ShippingScope (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:5518:2: error: sch-props-correct.2: Duplicate global type: Bulk (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:5561:2: error: sch-props-correct.2: Duplicate global type: WeightUOM (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:5604:2: error: sch-props-correct.2: Duplicate global type: CountryCode (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:6121:2: error: sch-props-correct.2: Duplicate global type: TransportMethod (Original global type found in file: AmazonPIES.xsd
)
PIES_5.0_02_01_2006.xsd:6168:2: error: sch-props-correct.2: Duplicate global type: Regulated (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:6212:2: error: sch-props-correct.2: Duplicate global type: PackagingGroupCode (Original global type found in file: AmazonPIES.
xsd)
PIES_5.0_02_01_2006.xsd:6258:2: error: sch-props-correct.2: Duplicate global type: ExtendedProductInformationCode (Original global type found in file:
 AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:6417:2: error: sch-props-correct.2: Duplicate global type: RegulationsExceptionCode (Original global type found in file: Amazo
nPIES.xsd)
PIES_5.0_02_01_2006.xsd:6466:2: error: sch-props-correct.2: Duplicate global type: PriceBreakUOM (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:6545:2: error: sch-props-correct.2: Duplicate global type: PriceUOM (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:6681:2: error: sch-props-correct.2: Duplicate global type: DescriptionCode (Original global type found in file: AmazonPIES.xsd
)
PIES_5.0_02_01_2006.xsd:6748:2: error: sch-props-correct.2: Duplicate global type: PriceType (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:6865:2: error: sch-props-correct.2: Duplicate global type: OQUOM (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:6974:2: error: sch-props-correct.2: Duplicate global type: GTINQualifier (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:7025:2: error: sch-props-correct.2: Duplicate global type: HazardousMaterialCode (Original global type found in file: AmazonPI
ES.xsd)
PIES_5.0_02_01_2006.xsd:7070:2: error: sch-props-correct.2: Duplicate global type: StateCode (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:7247:2: error: sch-props-correct.2: Duplicate global type: ContactType (Original global type found in file: AmazonPIES.xsd)
PIES_5.0_02_01_2006.xsd:7290:2: error: sch-props-correct.2: Duplicate global type: QualityGradeLevel (Original global type found in file: AmazonPIES.x
sd)
PIES_5.0_02_01_2006.xsd:13:2: error: sch-props-correct.2: Duplicate global element: PartInformation (Original global element found in file: AmazonPIES
.xsd)
TypeDefinitions.xsd:36:2: error: sch-props-correct.2: Duplicate global type: CurrencyCode (Original global type found in file: PIES_5.0_02_01_2006.xsd
)
Time to build schema type system: 3.525 seconds
BUILD FAILED

不幸的是,XSD文件都标记为机密,因此我无法在此处分享它们的内容。如果说任何人有任何预感,建议甚至是指导方向,请分享。

1 个答案:

答案 0 :(得分:1)

这可能是也可能不是答案,因为我不知道您的设置的具体细节,但问题强烈提醒我这一点,所以我认为它可能会有所帮助。请注意,这仅适用于使用XJC的JAXB。但是,这个问题的基础可能实际上是Apache Xerces和/或其JRE等价物中的错误,因此您选择的绑定框架可能不会产生影响。我遇到了JRE Xerces fork和最新的Xerces版本的问题,因此切换XML API实现对我来说也没有任何作用。

我有一个Maven项目,它简单地包含一个主模式,一些其他模式,主要包括(相同的命名空间)和一个绑定文件。然后我有任意数量的其他Maven项目,每个项目都有自己的模式导入主模式(不同的名称空间)。由于主模式及其包含的JAXB实体已经由第一个Maven项目生成,我也希望使用一个剧集文件来确保其他项目只是扩展它,而不是重新生成代码。这将允许我分发单独的罐子。

这是第一个主要项目的Maven pom(Maven 3):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>acme.xml</groupId>
    <artifactId>acme-xml-main</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <name>Acme XML main models</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <outputDirectory>${basedir}/target/classes-${compilerTarget}</outputDirectory>
        <testOutputDirectory>${basedir}/target/test-classes-${compilerTarget}</testOutputDirectory>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <!-- Filtering out any svn folders -->
                    <exclude>**/.svn</exclude>
                    <!-- Filtering out JAXB binding files -->
                    <exclude>**/*.xjb</exclude>
                    <!-- Filtering out XML catalogs -->
                    <exclude>**/*.cat</exclude>
                </excludes>
            </resource>
        </resources>
        <!-- First we need to generate Java source files based on the schemas -->
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb21-plugin</artifactId>
                <version>0.8.0</version>
                <executions>
                    <execution>
                        <id>xjc-generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>${basedir}/src/main/resources/acme/schemas</schemaDirectory>
                            <schemaLanguage>XMLSCHEMA</schemaLanguage>
                            <addCompileSourceRoot>true</addCompileSourceRoot>
                            <episode>true</episode>
                            <removeOldOutput>true</removeOldOutput>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <target>1.6</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <optimize>true</optimize>
                 <source>1.6</source>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
            </plugin>
        </plugins>
    </build>
</project>

模式与XJC生成的类一起打包。我想在其他“扩展”它的项目中引用这个项目(或更具体地说,它的jar)。

以下是Maven pom的一个延伸:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>acme.xml</groupId>
    <artifactId>acme-xml-sub</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <name>Acme XML sub models</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>acme.xml</groupId>
            <artifactId>acme-xml-main</artifactId>
            <version>1.0.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <outputDirectory>${basedir}/target/classes-${compilerTarget}</outputDirectory>
        <testOutputDirectory>${basedir}/target/test-classes-${compilerTarget}</testOutputDirectory>
        <!-- Resources to include into the packaged build -->
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <!-- Filtering out any svn folders -->
                    <exclude>**/.svn</exclude>
                    <!-- Filtering out JAXB binding files -->
                    <exclude>**/*.xjb</exclude>
                    <!-- Filtering out XML catalogs -->
                    <exclude>**/*.cat</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <!-- First we need to generate Java source files based on the schemas -->
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb21-plugin</artifactId>
                <version>0.8.2</version>
                <executions>
                    <execution>
                        <id>xjc-generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <!-- The extension flag needs to be on in order to use the episode file -->
                            <args>
                                <arg>-extension</arg>
                            </args>
                            <schemaDirectory>${basedir}/src/main/resources/acme/sub/schemas</schemaDirectory>
                            <schemaLanguage>XMLSCHEMA</schemaLanguage>
                            <addCompileSourceRoot>true</addCompileSourceRoot>
                            <!-- No need to generate an episode for this compilation... -->
                            <episode>false</episode>
                            <!-- The catalog makes sure we can resolve the main schema -->
                            <catalog>${basedir}/src/main/resources/acme/sub/schemas/catalog.cat</catalog>
                            <!-- Using the episode file from the main model jar -->
                            <episodes>
                                <episode>
                                    <groupId>acme.xml</groupId>
                                    <artifactId>acme-xml-main</artifactId>
                                    <version>1.0.0</version>
                                    <scope>compile</scope>
                                </episode>
                            </episodes>
                            <removeOldOutput>true</removeOldOutput>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <target>1.6</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <optimize>true</optimize>
                    <excludes>
                        <!-- This exclude avoids compiling the ObjectFactory class that -->
                        <!-- XJC insists on generating for the default package name of the main -->
                        <!-- namespace. Please leave this in to avoid any trouble. -->
                        <exclude>**/acme/main/ObjectFactory.java</exclude>
                    </excludes>
                 <source>1.6</source>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
            </plugin>
        </plugins>
    </build>
</project>

我遇到的第一个问题是无法找到导入子模式的主模式。让我们调用主模式main.xsd,假设它有目标名称空间http://www.acme.com,并且它的JAXB类放在包acme.main中,由绑定文件提供。

要解析模式,您可以使用目录。所以这是我尝试过的第一件事。在子项目中,我添加了一个包含以下内容的catalog.cat文件:

--
  Resolving the main XML Schema system ID.
--
SYSTEM "main.xsd" "classpath:/acme/schemas/main.xsd"

--
  Resolving the main namespace to the corresponding schema file.
--
PUBLIC "http://www.acme.com" "classpath:/acme/schemas/main.xsd"

您可以在上面的第二个Maven pom中看到catalog.cat。 我为SYSTEM ID尝试了一大堆不同的方法,但不管我做了什么,解决类路径上的模式似乎都没有用。所以我转而创建了一个自定义目录解析器,可以为我处理这个问题。这是我用主模式/ JAXB模型jar文件打包的Java类。我将这种依赖性添加到第一个Maven pom ......

<dependency>
    <groupId>com.sun.org.apache.xml.internal</groupId>
    <artifactId>resolver</artifactId>
    <version>20050927</version>
    <scope>provided</scope>
</dependency>

...并创建了一个自定义目录解析器,它始终将某些系统ID和主命名空间URI链接到我的主模式。

然后我将第二个Maven pom的XJC生成配置调整为:

<!-- First we need to generate Java source files based on the schemas -->
<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb21-plugin</artifactId>
    <version>0.8.2</version>
    <!-- Need this dependency for the custom catalog resolver -->
    <dependencies>
        <dependency>
            <groupId>acme.xml</groupId>
            <artifactId>acme-xml-main</artifactId>
            <version>1.0.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>xjc-generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <!-- The extension flag needs to be on in order to use the episode file -->
                <args>
                    <arg>-extension</arg>
                </args>
                <schemaDirectory>${basedir}/src/main/resources/acme/sub/schemas</schemaDirectory>
                <schemaLanguage>XMLSCHEMA</schemaLanguage>
                <addCompileSourceRoot>true</addCompileSourceRoot>
                <!-- No need to generate an episode for this compilation... -->
                <episode>false</episode>
                <!-- The catalog makes sure we can resolve the main schema -->
                <catalog>${basedir}/src/main/resources/acme/sub/schemas/catalog.cat</catalog>
                <catalogResolver>acme.xml.tools.AcmeCatalogResolver</catalogResolver>
                <!-- Using the episode file from the general model bundle -->
                <episodes>
                    <episode>
                        <groupId>acme.xml</groupId>
                        <artifactId>acme-xml-main</artifactId>
                        <version>1.0.0</version>
                        <scope>compile</scope>
                    </episode>
                </episodes>
                <removeOldOutput>true</removeOldOutput>
            </configuration>
        </execution>
    </executions>
</plugin>

通过这个以及目录解析器的一些控制台输出来查明发生了什么,最终可以从类路径解析主模式。但事情还没有准备好。

每当我在主模式中使用包含时,为了清楚起见确保它可以在不同的文件和组定义中分解,我会发现XML处理器抱怨重复的定义。似乎各种文件将通过不同的路径解决(一些系统ID,其他命名空间,一些完整路径,其他相对......),这似乎会导致问题。虽然这些不同的路径指向基本上相同的文件,但是从它加载的任何内容都将被XML处理器视为单独的定义。似乎XML验证API并不擅长创建一个连贯的Schema对象,只是不断查找内容,而不是意识到它通过不同的路径查看同一个文件。

所以我在自定义目录解析器中添加了更多内容,以确保始终以相同的方式处理所包含的文件。以下是结果类:

package acme.xml.tools;

import com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.InputSource;

public class AcmeCatalogResolver extends CatalogResolver {

    private final static String namespace = "http://www.acme.com";
    private final static Set<String> systemIDs;
    static {
        final Set<String> temp = new HashSet<String>();
        temp.add("main.xsd");
        temp.add("Main.xsd");
        temp.add("MAIN.xsd");
        systemIDs = Collections.unmodifiableSet(temp);
    }

    @Override
    public InputSource resolveEntity(String publicId, String systemId) {

        //If the public ID is the main namespace, get the input source
        //based on the system ID
        if(publicId != null && publicId.trim().equals(namespace)) {
            final InputSource source = getInputSource(systemId);
            source.setPublicId(publicId);
            return source;
        }

        //Otherwise, check if the system ID is one of the allowed references
        //to the main schema and return that
        if(systemId != null && systemIDs.contains(systemId.trim())) {
            final InputSource source = getMainSchemaInputSource();
            source.setPublicId(publicId);
            return source;
        }

        //Still nothing: check for file or jar URIs to the main schema
        if(systemId != null && (systemId.startsWith("file:") || 
                systemId.startsWith("jar:"))) {
            boolean match = false;
            for(final String id : systemIDs) {
                match = systemId.endsWith(id);
                if(match)
                    break;
            }
            if(match) {
                final InputSource source = getMainSchemaInputSource();
                source.setPublicId(publicId);
                return source;
            }
        }

        //Final option: use only the system ID to find the source
        if(systemId != null && !systemId.isEmpty()) {
            final InputSource source = getInputSource(systemId);
            source.setPublicId(publicId);
            return source;
        }

        //This should not be necessary, but just in case...
        return super.resolveEntity(publicId, systemId);

    }

    @Override
    public Source resolve(String href, String base) throws TransformerException {

        if(href != null && href.trim().equals(namespace)) {
            final StreamSource source = getMainSchemaSource();
            source.setPublicId(href);
            return getMainSchemaSource();
        }

        return super.resolve(href, base);

    }

    @Override
    public String getResolvedEntity(String string, String string1) {

        return super.getResolvedEntity(string, string1);

    }

    private InputSource getInputSource(String systemId) {

        final int lastSlash = systemId.lastIndexOf('/');
        if(lastSlash >= 0)
            systemId = systemId.substring(lastSlash + 1);
        final int lastBackSlash = systemId.lastIndexOf('\\');
        if(lastBackSlash >= 0)
            systemId = systemId.substring(lastBackSlash + 1);

        final URL url = 
            this.getClass().getResource("/acme/schemas/" + systemId);

        final InputStream stream = 
            this.getClass().getResourceAsStream("/acme/schemas/" + systemId);

        final InputSource source = new InputSource(stream);
        source.setSystemId(url.toString());

        return source;

    }

    private InputSource getMainSchemaInputSource() {

        final URL url = 
            this.getClass().getResource("/acme/schemas/main.xsd");

        final InputStream stream = 
            this.getClass().getResourceAsStream("/acme/schemas/main.xsd");

        final InputSource source = new InputSource(stream);
        source.setSystemId(url.toString());

        return source;

    }

    private StreamSource getGeneralSchemaSource() {

        final URL url = 
            this.getClass().getResource("/acme/schemas/main.xsd");

        final InputStream stream = 
            this.getClass().getResourceAsStream("/acme/schemas/main.xsd");

        final StreamSource source = new StreamSource(stream);
        source.setSystemId(url.toString());

        return source;

    }

}

有了这个,我终于明白了。我希望这可能在某种程度上启发了解决方案的途径。

是的,地狱很肮脏。我正在扩展太阳内部课程。我基本上只是使用目录文件将主模式或其命名空间的请求定向到一个硬编码整个交易的hacky类。但至少它是这样工作的。 因为显然Sun无法解决其JAXP实施中已经开放多年的问题!而且我认为甲骨文也不会给人一种飞行的麻烦。但是你最好确定他们会试图强迫XML扼杀你的任何程序互动,这种互动很可能会离开主板。 Apache的Xerces实现也没有解决任何问题。鉴于W3C XML Schema规范的灾难,我并不能真正责怪他们。

对于最后一点感到抱歉,我只是需要时不时地发泄,事实证明,使用Java中的XML Schema做一些微不足道的事情会立即变成精神折磨。如果JAXB不那么有用,我就不会为此烦恼。