1

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

我也尝试过 xmlbeansscomp命令,但也失败了。

我正在运行的命令是:

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

哪个输出:

[新江城]

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 文件都被标记为机密,所以我无法在这里分享它们的内容。话虽这么说,如果有人有任何预感,建议甚至是方向,请分享。

4

1 回答 1

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;

    }

}

有了这个,我终于让它工作了。我希望这可以在某种程度上为您提供解决方案的途径。

是的,脏得要命。我正在扩展 Sun 内部课程。我基本上只是使用目录文件将对主模式或其名称空间的请求定向到一些硬编码整个交易的 hacky 类。但至少它是这样工作的。由于显然 Sun 无法解决已开放多年的 JAXP 实现中的问题!而且我认为甲骨文也不会胡说八道。但是您最好确保他们会尝试将 XML 强加于您的喉咙以进行任何敢于离开主板的程序交互。Apache 的 Xerces 实现也没有解决任何问题。考虑到 W3C XML Schema 规范的灾难,我并不能真正责怪他们。

对最后一点感到抱歉,当事实证明,在 Java 中使用 XML Schema 做琐碎的事情之外的任何事情都会立即变成精神折磨时,我只需要不时发泄一下。如果 JAXB 不是那么有用,我不会为此烦恼。

于 2013-01-21T15:17:30.813 回答