Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

xsd - JAXB schema to Java Different XmlRootElement name and Class name

I have a xsd schema from which I'm generating some java classes. I'm using jaxb for the generation.

I want to be able to generate a class annotated with @XmlRootElement, but I want the @XmlRootElement name property to be different than the name of the generated class.

In my xsd I'm defining the following:

<xs:element name="customer">
    <xs:complexType>
        <xs:sequence>
         ....
        </xs:sequence>
     </xs:complexType>
</xs:element>

This piece of code generates the following java class:

@XmlRootElement(name = "customer")
public class Customer {
...
}

The name property of the @XmlRootElement is the same as the name of the generated Class. I want the generated class name to be CustomerRequest.

I've tryed to use the jaxb:class definition to change the classe name. Indeed this option changes the class name but removes the @XmlRootElement annotation, and I need it to be present.

The following xsd:

<xs:element name="customer">
    <xs:complexType>
        <xs:annotation>
                <xs:appinfo>
                    <jaxb:class name="CustomerRequest"/>
                </xs:appinfo>
            </xs:annotation>
        <xs:sequence>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Generates this class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {

})
public class CustomerRequest {
}

How can I make the property name of the @XmlRootElement annotation different from the generated class name without loosing the annotation?

Solution update: User Xstian proposed the correct solution using external bindings. Just for further reference, I'll update my own post with the solution converted for using inline bindings:

 <xs:element name="customer">
        <xs:complexType>
            <xs:annotation>
                <xs:documentation>Request object for the operation that checks if a customer profile exists.</xs:documentation>
                <xs:appinfo>
                        <annox:annotate>
                            <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="customer"/>
                        </annox:annotate>
                        <jaxb:class name="CustomerRequest"/>
                    </xs:appinfo>
                </xs:annotation>
            <xs:sequence>
            </xs:sequece>
    </xs:complexType>
</xs:element>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest you to use this bindings

<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="../your.xsd">

        <bindings node="//xs:element[@name='customer']//xs:complexType">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="customer" namespace="yourNamespaceIfYouWant">
                </annox:annotate>
            </annox:annotate>
            <class name="CustomerRequest"/>
        </bindings>

    </bindings>
</bindings>

Class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "header"
})
@XmlRootElement(name = "customer", namespace = "yourNamespaceIfYouWant")
public class CustomerRequest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...