本文介绍了Castor 能否处理从基本 XSD 导入的多个 XSD 的类生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个重复使用相同实体的 XSD.例如,ProductPurchaseRequest.xsdProductQuoteRequest.xsd 的 XSD 都有一个 标签来描述产品有问题.为此,我创建了一个 Product.xsd 文件来定义 标记以及 ProductPurchaseRequest.xsdProductQuoteRequest.xsd 使用 `.

I have several XSDs that reuse the same entities. For example, both the XSDs for the ProductPurchaseRequest.xsd and ProductQuoteRequest.xsd both have a <product> tag in them to describe the product in question. For this reason I created a Product.xsd file to define the <product> tag and both ProductPurchaseRequest.xsd and ProductQuoteRequest.xsd import the Product.xsd with a `.

我想使用 Castor 从这些 XSD 生成 Java 类,并且让它们使用相同的类来表示 Product,以便我可以重用相同的逻辑来将它们映射到我们模型的 ProductModel 类.

I would like to use Castor to generate Java classes from theses XSDs, and for both of them to use the same class for representing the Product so that I could reuse the same logic for mapping them to our model's ProductModel class.

Castor 能做到吗?如果是这样,它的 Ant 任务语法是什么.如果没有,也许 JAXB 是更好的选择?

Can Castor do this? If so, what would be the Ant task syntax for it. If not, would perhaps JAXB be a better alternative?

推荐答案

所以我能够使用 JAXB 参考实现使整个事情正常工作.诀窍是意识到从 JAXB 站点 的下载是一个下载器 不是 实际的图书馆!双击接受许可,库将下载到本地.创建一个测试文件夹 (JAXB_TEST) 并将所有下载的 .jar 复制到 lib 子文件夹.我将所有 XSD 放在 XSD 子文件夹中.之后,我运行了以下 Ant build.xml 文件.

So I was able to get the whole thing working using the JAXB reference implementation. The trick was to realise that the download from JAXB site is a downloader and not the actual libraries! Double-click to accept the licence and the libraries will download locally. Created a test folder (JAXB_TEST) and copied all the downloaded .jars to a lib subfolder. I put all my XSDs in XSD subfolder. After that I ran the following Ant build.xml file.

<?xml version="1.0"?>
<project name="jaxb_test" basedir="." default="package" >

    <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
        <classpath>
            <fileset dir="./lib" includes="*.jar" />
        </classpath>
    </taskdef>

    <target name="generate">
        <delete dir="./gen-src" />
        <mkdir dir="./gen-src" />

        <xjc destdir="./gen-src" language="XMLSCHEMA" package="com.mmm" >
            <schema dir="./xsd" includes="EPC*.xsd" />
        </xjc>
    </target>

    <target name="compile" depends="generate" >
        <delete dir="./bin" />
        <mkdir dir="./bin" />

        <javac srcdir="./gen-src" destdir="./bin" includeantruntime="false" />
    </target>

    <target name="package" depends="compile" >
        <delete dir="./dist" />
        <mkdir dir="./dist" />
        <jar destfile="./dist/jaxb-gen.jar" basedir="./bin" />
    </target>


</project>

我遇到的唯一问题是我有一个名为 <product> 的根标签的 xsd 匿名扩展了 Product 类型以添加​​一个 版本 属性(我总是喜欢在我的根标记上有),这导致了 JAXB 的名称冲突.因此,我将匿名类型转换为命名类型(即 TopLevelProduct)并将根设置为该类型,JAXB 对此很满意.

The only problem I had was that I had an xsd with a root tag called <product> that anonymous extended the Product type to add a version attribute (which I always like to have on my root tag's) which was causing a name conflict for JAXB. So instead, I turned the anonymous type into a named type (i.e. TopLevelProduct) and set the root to that type and JAXB was happy with that.

这篇关于Castor 能否处理从基本 XSD 导入的多个 XSD 的类生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 16:49