注意:我是EclipseLink JAXB (MOXy)负责人,也是JAXB 2 (JSR-222)专家组的成员。
卡斯特能做到吗?如果是这样,它的 Ant 任务语法是什么。如果不是,也许 JAXB 会是更好的选择吗?
以下是如何使用 JAXB 完成此操作的示例:
产品
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/Product"
xmlns:tns="http://www.example.org/Product"
elementFormDefault="qualified">
<element name="product">
<complexType>
<sequence>
<element name="id" type="string"/>
<element name="name" type="string"/>
</sequence>
</complexType>
</element>
</schema>
由于多个 XML 模式导入 Product.xsd,我们可以利用情节文件,以便与 Product.xsd 对应的类只生成一次。
xjc -d out -episode product.episode Product.xsd
ProductPurchaseRequest.xsd
下面是导入 Product.xsd 的 XML 模式示例:
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/ProductPurchaseRequest"
xmlns:tns="http://www.example.org/ProductPurchaseRequest"
xmlns:prod="http://www.example.org/Product"
elementFormDefault="qualified">
<import namespace="http://www.example.org/Product" schemaLocation="Product.xsd"/>
<element name="purchase-request">
<complexType>
<sequence>
<element ref="prod:product" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
</schema>
当我们从这个 XML 模式生成类时,我们将引用我们在从 Product.xsd 生成 Java 类时创建的情节文件。
xjc -d out ProductPurchaseRequest.xsd -extension -b product.episode
ProductQuoteRequest.xsd
下面是另一个导入 Product.xsd 的 XML 模式示例:
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/ProductQuoteRequest"
xmlns:tns="http://www.example.org/ProductQuoteRequest"
xmlns:prod="http://www.example.org/Product"
elementFormDefault="qualified">
<import namespace="http://www.example.org/Product" schemaLocation="Product.xsd"/>
<element name="quote">
<complexType>
<sequence>
<element ref="prod:product"/>
</sequence>
</complexType>
</element>
</schema>
Again when we generate classes from this XML schema we will reference the episode file we created when we generated Java classes from Product.xsd.
xjc -d out ProductQuoteRequest.xsd -extension -b product.episode
For More Information