I’m currently trying to figure out why JAXB marshaller uses Java member variable convention as opposed to follow the XmlType annotation.
Here’s the situation:
- Third-party gave us XSD
- We use JDK tools to generate Java classes
- The generated Java classes produced correct annotation: @XmlType(name = “XML_DOCUMENT_TYPE”)
But when I tried to marshal the class back to XML, JAXB converts it to <xmlDocumentType> instead of <XML_DOCUMENT_TYPE>
Any idea why? (If so, how can I fix this?)
Update: to clarify, the issue occurred at the top/root level element, not at the sub element/member variable.
UPDATE (based on comment by xandross
You can use
@XmlRootElementto control the root element name:Alternatively you can wrap the root object in an instance of
JAXBElementto supply root element information.UPDATE (based on comment by Mohamed Mansour)
In JAXB classes correspond to XML types, and fields/properties correspond to XML attributes/elements. This makes sense when you consider there may exist an address type:
and multiple elements (with different names) that are of that type:
You can control the name of the element/attribute that a property maps to with the
@XmlElement/@XmlAttributeannotations:or
If the property is not annotated it is treated as
@XmlElementand the element name is derived from the property name.