在外部绑定文件中设置XJC javaType适配器

时间:2014-07-25 09:28:25

标签: xml jaxb xsd adapter xjc

我的XSD文件包含:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc"
elementFormDefault="qualified"
targetNamespace="http://example.org/">

<xsd:complexType name="Certificate">
    <xsd:sequence>
        <xsd:element name="certificate" type="xsd:base64Binary">
            <xsd:annotation>
                <xsd:appinfo>
                    <xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
                </xsd:appinfo>
            </xsd:annotation>
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>

</xsd:schema>

<xsd:complexType name="User">
    <xsd:sequence>
        <xsd:element name="certificate" type="xsd:base64Binary">
            <xsd:annotation>
                <xsd:appinfo>
                    <xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
                </xsd:appinfo>
            </xsd:annotation>
        </xsd:element>
        <xsd:element name="name" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Server">
    <xsd:sequence>
        <xsd:element name="document" type="xsd:base64Binary" />
    </xsd:sequence>
</xsd:complexType>

我想将xjc javaType适配器导出到外部.xjb文件。在此文件中,我想为certificate类型的所有xsd:base64Binary元素设置此适配器,但document复杂类型中的Server元素不适用xsd:base64Binary元素} type。

我该怎么做?

1 个答案:

答案 0 :(得分:12)

中更改您的xsd
<xsd:complexType name="Certificate">
    <xsd:sequence>
        <xsd:element name="certificate" type="xsd:base64Binary"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="User">
    <xsd:sequence>
        <xsd:element name="certificate" type="xsd:base64Binary"/>
        <xsd:element name="name" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Server">
    <xsd:sequence>
        <xsd:element name="document" type="xsd:base64Binary" />
    </xsd:sequence>
</xsd:complexType>

并添加此绑定配置

<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:annox="http://annox.dev.java.net"
    xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
    <bindings schemaLocation="yourXSD.xsd">

            <bindings node="//xsd:complexType[@name='Certificate']//xsd:sequence//xsd:element[@name='certificate']">
                <xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
            </bindings>
            <bindings node="//xsd:complexType[@name='User']//xsd:sequence//xsd:element[@name='certificate']">
                <xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
            </bindings>

    </bindings>
</bindings>