重用生成的JAXB类

在本文中,我将演示如何利用– XJC扩展来重用以前从XML模式生成的类。 当其他XML架构导入XML架构并且您不想每次都生成相同的类时,这很有用。

导入的架构(Product.xsd)

以下XML模式代表有关产品的基本信息。 产品是此示例域中的常见概念,因此我决定定义一种可以被其他模式利用的表示形式,而不是让每个模式都定义自己的产品信息表示形式。

<?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调用演示了如何生成称为product的情节文件。 情节以及生成的类:

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类时创建的情节文件。 如果我们未指定情节文件,则将为ProductPurchaseRequest.xsdProduct.xsd生成类:

另一个导入模式(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>

同样,当我们从此XML模式生成类时,我们将引用从Product.xsd生成Java类时创建的情节文件。

xjc -d out ProductQuoteRequest.xsd -extension -b product.episode

它是如何工作的? (product.episode)

对于你们中的那些人来说,这很奇怪。 XJC生成的情节文件实际上只是一个用于自定义类生成的标准JAXB绑定文件。 生成的绑定/情节文件包含一些条目,这些条目告诉XJC此类型的类已经存在。 您可以手工编写此文件,但是XJC的-episode标志可以帮您完成。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb ">
<!--

This file was generated by the JavaTM Architecture for XML Binding
(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 See 
<a href=" http://java.sun.com/xml/jaxb "> http://java.sun.com/xml/jaxb </a> 
Any modifications to this file will be lost upon recompilation of the 
source schema. 
Generated on: 2011.11.02 at 03:40:10 PM EDT 

-->
<bindings scd="x-schema::tns"
xmlns:tns=" http://www.example.org/Product ">
<schemaBindings map="false"/>
<bindings scd="tns:product">
<class ref="org.example.product.Product"/>
</bindings>
</bindings>
</bindings>

参考: Java XML和JSON绑定博客中的JCG合作伙伴 Blaise Doughan 重用了生成的JAXB类

相关文章 :


翻译自: https://www.javacodegeeks.com/2011/12/reusing-generated-jaxb-classes.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值