简体   繁体   中英

JAXB XJC bindings: renaming @XmlRootElement and @XmlType together

I have the following XML schema:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="cg" type="cgType"/>

    <xsd:complexType name="cgType">
        <xsd:sequence>
            <xsd:element name="code" type="upperCaseString" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="action" type="cgAction" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="param" type="xsd:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="automated" type="xsd:boolean" default="false" use="optional"/>
    </xsd:complexType>

    <xsd:simpleType name="cgAction">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="TAKE"/>
            <xsd:enumeration value="CLEAR"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="upperCaseString">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="[A-Z]*"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

EDIT: I also have the following global bindings:

<?xml version="1.0" encoding="UTF-8" ?>
<jaxb:bindings
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        jaxb:version="2.1">

    <jaxb:bindings schemaLocation="cg.xsd" node="/xsd:schema">
        <jaxb:globalBindings>
            <xjc:simple/>
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>

Using XJC to generate classes works as expected, with a class named Cg that has both @XmlType and @XmlRootElement annotations. I want to rename that class to be CG , so I changed the binding file to be:

<?xml version="1.0" encoding="UTF-8" ?>
<jaxb:bindings
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        jaxb:version="2.1">

    <jaxb:bindings schemaLocation="cg.xsd" node="/xsd:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="foo.bar.cg"/>
        </jaxb:schemaBindings>

        <jaxb:bindings node="//xsd:element[@name='cg']">
            <jaxb:class name="CG"/>
        </jaxb:bindings>
        <jaxb:bindings node="//xsd:complexType[@name='cgType']">
            <jaxb:class name="CG"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

This causes XJC to complain about a collision in the ObjectFactory class between the two CG classes. If I remove the binding on the xsd:element , XJC will now create a CG class as expected, however it misses out the @XmlRootElement annotation. I've experimented with various combinations of bindings on the element and complexType but have been unable to simply rename the default generated class.

How can I rename both the element and the complexType at once?

Both the element and complexType are their own java classes. Using no binding file you should get the result of 2 class files: Cg.java and CgType.java . In your bindings your trying to rename both of those to CG.java which of course is not possible.

What your probably looking for is to rename the complexType to CGType in which case your bindings should read:

...
    <jaxb:bindings node="//xsd:element[@name='cg']">
        <jaxb:class name="CG"/>
    </jaxb:bindings>
    <jaxb:bindings node="//xsd:complexType[@name='cgType']">
        <jaxb:class name="CGType"/>
    </jaxb:bindings>
...

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