I am using JAXB to marshal Java objects into XML elements. My object hierarchy is complex, following a base class and derived class structure. While I am able to generate the XML document from my Java classes, I am facing an issue with defining the order of XML elements within the base and derived classes.
My requirement is to define a custom order for elements, combining both the base class and the derived class, with the base class elements appearing first. I understand that the propOrder
attribute can be used to define the sort order, but with inheritance, the sort order defined in the derived class takes precedence over that of the base class.
I also considered marking the base class as @XmlTransient
, but this doesn't solve my issue, as I need the base class to be annotated with @XmlElement
to provide custom names for its fields.
Has anyone in the community encountered a similar issue and found a solution? Any guidance would be greatly appreciated!
Sample class from my project:
Base Class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ClaimSegment", propOrder = { "prescSvcRefNumberQualifier", "prescSvcRefNumber",
"preferredProductLoop" })
@XmlSeeAlso({ D0ClaimSegment.class, F6ClaimSegment.class })
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = D0ClaimSegment.class, name = "D0"),
@JsonSubTypes.Type(value = F6ClaimSegment.class, name = "F6") })
public abstract class ClaimSegment implements Serializable {
private static final long serialVersionUID = 3392171783371612817L;
@XmlElements({ @XmlElement(name = "D0PreferredProductLoop", type = D0PreferredProductLoop.class),
@XmlElement(name = "F6PreferredProductLoop", type = F6PreferredProductLoop.class) })
protected List\<PreferredProductLoop> preferredProductLoop;
@XmlAttribute(name = "PrescSvcRefNumberQualifier", required = true)
protected String prescSvcRefNumberQualifier;
@XmlAttribute(name = "PrescSvcRefNumber", required = true)
protected String prescSvcRefNumber;
@XmlAttribute(name = "MedicaidSubrogationInternalControlNumberTransactionControlNumberICNTCN")
protected String medicaidSubrogationInternalControlNumberTransactionControlNumberICNTCN;
Derived Class:
@XmlRootElement(name = "F6ClaimSegment")
public class F6ClaimSegment extends ClaimSegment {
private static final long serialVersionUID = 2187013021743793448L;
@XmlElement(name = "PreferredProductLoop")
@JsonProperty("preferredProductLoop")
protected List\<F6PreferredProductLoop> preferredProductLoop;
@XmlAttribute(name = "FormularyAlternativeProductCount")
protected String formularyAlternativeProductCount;
@XmlAttribute(name = "MinimumAgeQualifier")
protected String minimumAgeQualifier;
@XmlAttribute(name = "MinimumAge")
protected String minimumAge;
@XmlAttribute(name = "MaximumAgeQualifier")
protected String maximumAgeQualifier;
@XmlAttribute(name = "MaximumAge")
protected String maximumAge;
My expectation of the xml output is like something below,
<ClaimSegment PrescSvcRefNumberQualifier="01" PrescSvcRefNumber="123847" MinimumAgeQualifier="01" MinimumAge="10" MaximumAgeQualifier="01" MaximumAge="60" />