开发者

JAXB marshaller using Java member variable convention

I'm currently trying to figure out why JAXB marshaller uses Java member variable convention as oppos开发者_如何学Pythoned 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 @XmlRootElement to control the root element name:

@XmlRootElement(name="XML_DOCUMENT_TYPE")
public class Foo {
  ...
}

Alternatively you can wrap the root object in an instance of JAXBElement to 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:

<xs:complexType name="address">
    <xs:sequence>
        <xs:element name="street" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

and multiple elements (with different names) that are of that type:

<xs:complexType name="customer">
    <xs:sequence>
        <xs:element name="billing-address type="address"/>
        <xs:element name="shipping-address type="address"/>
    </xs:sequence>
</xs:complexType>

You can control the name of the element/attribute that a property maps to with the @XmlElement/@XmlAttribute annotations:

@XmlElement(name="shipping-address")
public getShippingAddress() {
    return shippingAddress;
}

or

@XmlElement(name="ShippingAddress")
public getShippingAddress() {
    return shippingAddress;
}

If the property is not annotated it is treated as @XmlElement and the element name is derived from the property name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜