But I would like this " /> But I would like this " /> But I would like this "/>

How do I change the case of XSD element name when generated as @XmlElement via xjc

1.6k Views Asked by At

I have a schema where element names are defined in PascalCase eg:

<xsd:element name="EmployeeName" minOccurs="0" maxOccurs="1">

But I would like this to generate as:

@XmlElement(name = "employeeName")

I know this sounds slightly strange but it will then allow me to use Jackson JAXB annotation support to have my JSON generated in camelCase.

Is this possible?

1

There are 1 best solutions below

1
Edwin Buck On

Yes, it is possible to change the XML element name to (nearly) whatever you want by way of the annotation directives.

In this example, "price" is renamed to "itemprice". Java is not case insensitive, so your camel casing will be honored.

 //Example: Code fragment
 public class USPrice {
     @XmlElement(name="itemprice")
     public java.math.BigDecimal price;
 }

 <!-- Example: Local XML Schema element -->
 <xs:complexType name="USPrice"/>
   <xs:sequence>
     <xs:element name="itemprice" type="xs:decimal" minOccurs="0"/>
   </sequence>
 </xs:complexType>

This example comes from the JAXB javadocs.

Note that JAXB support has a few "how to use it" workflows, "XSD to generate supporting Java classes", and "Java classes to generate an XSD". I prefer the latter, but you may be using the former. If you are, then you need to alter your XSD to have camelCase element names there.

There is no "use my XSD to generate Java classes, but with some overrides" workflow. Perhaps that's where the confusion comes from. Likewise, there is no "Use Java class annotations to generated XSD documents, but with some overrides". What you specify is going to be what you get, name-wise.