简体   繁体   中英

How to create the binding file for changing complexType to simpleType

I had a field in XSD schema:

<xs:element name="magicField">
    <xs:simpleType>
        <xs:restriction base="xs:int">
            <xs:totalDigits value="8"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>

In java class was generated a field:

int magicField;

But then, xsd schema has changed to:

<xs:element name="magicField">
    <xs:complexType>
        <xs:simpleContent>
            <xs:restriction base="int_code">
                <xs:totalDigits value="8"/>
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

Where "int_code":

<xs:complexType name="int_code">
    <xs:simpleContent>
        <xs:extension base="xs:positiveInteger">
            <xs:attribute name="name"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

And JAXB generates the following code:

MagicField magicField;

and MagicField.java in additional.

But I still need only

int magicField;

in generated code.
Attribute "name" won't be used at all.

I can't change the schema, my way is only binding file (xjb).

How I should create the binding to re-map "int_code" type and get "magicField" as simpleType ( int in code), not complexType ( magicField in code)?

As I understand I also need a custom XmlAdapter, not only binding rule.

Thanks in advance.

You can use following instead of creating a complex type int_code:

<xs:element name="magicField">
<xs:complexType>
    <xs:simpleContent>
        <xs:restriction base="xs:positiveInteger">
            <xs:totalDigits value="8"/>
        </xs:restriction>
    </xs:simpleContent>
</xs:complexType>

Or a better way you can create a simpleType for magicField as below:

   <xs:element name="magicField">
    <xs:simpleType>
        <xs:restriction base="xs:positiveInteger">
            <xs:totalDigits value="8" />
        </xs:restriction>
    </xs:simpleType>
</xs:element>

Hope this helps.

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