简体   繁体   中英

JAXB auto-generation from XSD: Use different class for particular element

I am generating Java classes from xsd file. I have tens of elements/classes which are working perfectly. However just for one class I want to use my own class, not the generated one.

    <xs:element name="mytype">
    <xs:complexType>
        <xs:all>
            <xs:element ref="mytype" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="type" use="required">
            <xs:simpleType>
                <xs:union>
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:pattern value="somepattern"/>                                
                        </xs:restriction>
                    </xs:simpleType>
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="enum1"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:union>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="t2" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:double"/>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="t1" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:double"/>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>

In order to set these I read some sources and defined xjb file as follows:

<jaxb:bindings schemaLocation="schema.xsd" node="//xs:schema">
    <jaxb:bindings node="xs:element[@name='mytype']">
    <jaxb:class implClass="com.package.MyType" name="MyType" />
    </jaxb:bindings>
</jaxb:bindings>

In short, I just want to use my class com.package.MyType to handle mytype element in the xsd file. I tried some other features like adapters but couldn't manage to solve the problem.

When I tried attached approach, I got incompatible types error on ObjectFactory.java and MyType.java (which is generated). because it tries to return my type but it is not a child class of generated type.

this is the line on ObjectFactory.java:

public com.autogenerate.MyType createMyType() {
    return new com.package.MyType();
}

this is the line on com.autogenerate.MyType.java:

public com.autogenerate.MyType getMyType() {
    return mytype;    //com.package.MyType
}

Try:

<jaxb:class ref="com.package.MyType"/>

This should use com.package.MyType instead of generating com.autogenerate.MyType .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM